Vim Cheat Sheet

Vim commands at a glance.

Save and quit

CommandDoes
:wSave
:qQuit (fails if there are unsaved changes)
:wq or :x or ZZSave and quit
:q!Quit and discard changes
:w newname.txtSave as

Stuck? Press Esc to return to Normal mode first, then type the command.

Modes

Normal (move and run commands, the default), Insert (type text: i before the cursor, a after, I/A at line start/end, o/O new line below/above), Visual (select: v characters, V lines, Ctrl+v block), and Command-line (:).

Moving around

KeysMove
h j k lLeft, down, up, right
w / b / eNext word / previous word / end of word
0 / ^ / $Line start / first non-blank / line end
gg / G / 42GFirst line / last line / line 42
%Matching bracket
Ctrl+d / Ctrl+uHalf page down / up
fx / txJump to / just before the next x on the line

Editing

Operators combine with motions: d delete, c change, y yank (copy). So dw deletes a word, c$ changes to the end of the line, y3j copies this and the next three lines, ci" changes the text inside quotes and dap deletes a paragraph. Doubling an operator acts on the whole line: dd, yy, cc. Other essentials: x delete a character, p/P paste after/before, u undo, Ctrl+r redo, . repeat the last change, >>/<< indent, J join lines, ~ toggle case.

Search and replace

/pattern        search forward (n = next, N = previous)
?pattern        search backward
*               search for the word under the cursor
:s/old/new/     replace first match on the line
:%s/old/new/g   replace in the whole file
:%s/old/new/gc  ... asking for confirmation each time
:noh            clear the highlight

Vim patterns are regular expressions with their own escaping (\( groups, \+); start a pattern with \v ("very magic") to use the familiar syntax. Test expressions in the regex tester.

Files, buffers and windows

:e file open, :ls list buffers, :bn/:bp next/previous buffer, :sp/:vsp split horizontally/vertically, Ctrl+w then a direction key to switch windows.

Macros

qa starts recording into register a, q stops, @a plays it back and 10@a plays it ten times. Ideal for repeating the same edit on many lines.

FAQ

How do I exit Vim?

Press Esc, then type :q! and Enter to quit without saving, or :wq to save and quit.

How do I paste without auto-indent messing it up?

Run :set paste before pasting and :set nopaste after, or paste from the system clipboard with "+p if your Vim has clipboard support.