Catalog / Vim Cheatsheet

Vim Cheatsheet

A comprehensive cheat sheet for Vim, covering essential commands, modes, navigation, editing, and advanced features to boost your coding efficiency.

Basic Navigation

Movement

h

Move cursor left

j

Move cursor down

k

Move cursor up

l

Move cursor right

w

Move to the beginning of the next word

b

Move to the beginning of the previous word

0

Move to the beginning of the line

$

Move to the end of the line

Scrolling

Ctrl + d

Scroll down half a page

Ctrl + u

Scroll up half a page

Ctrl + f

Scroll forward one full page

Ctrl + b

Scroll backward one full page

Line Navigation

gg

Go to the first line of the file

G

Go to the last line of the file

:[line_number]

Go to a specific line number (e.g., :10 goes to line 10)

Editing Commands

Basic Editing

i

Insert before the cursor

a

Append after the cursor

o

Open a new line below the current line

O

Open a new line above the current line

x

Delete character under cursor

dd

Delete the current line

Copy, Paste, and Delete

yy

Yank (copy) the current line

p

Paste after the cursor

P

Paste before the cursor

dw

Delete word

d$

Delete to end of line

D

Delete to end of line

Undo and Redo

u

Undo last change

Ctrl + r

Redo last undone change

.

Repeat the last command

Search and Replace

Searching

/pattern

Search for pattern forward

?pattern

Search for pattern backward

n

Find next occurrence

N

Find previous occurrence

Replacing

:%s/old/new/g

Replace all occurrences of old with new in the file

:%s/old/new/gc

Replace all occurrences of old with new in the file, with confirmation

:[range]s/old/new/g

Replace all occurrences of old with new within the specified range (e.g., :1,10s/old/new/g)

Visual Mode Search & Replace

  1. Enter Visual mode (v, V, or Ctrl-v).
  2. Select the text you want to operate on.
  3. Type :. Vim will automatically populate the command line with :'<,'>.
  4. Enter the substitution command: :'<,'>s/old/new/g.
  5. Add c flag to confirm each substitution: :'<,'>s/old/new/gc.

Saving and Exiting

Saving

:w

Save the current file

:w filename

Save the current file as filename

Exiting

:q

Quit Vim (only if no changes have been made)

:q!

Quit Vim, discard all changes

:wq

Save and quit

:x

Save and quit (only if changes have been made)

:ZZ

Save and quit (if changes have been made)

Combining Saving and Exiting

You can combine saving and exiting commands. For example, :wq will save the file and then quit Vim. If you want to save the file under a new name and exit, you would use :w filename followed by :q.