Vim Cheat Sheet
Vim commands at a glance.
Vim commands at a glance.
| Command | Does |
|---|---|
:w | Save |
:q | Quit (fails if there are unsaved changes) |
:wq or :x or ZZ | Save and quit |
:q! | Quit and discard changes |
:w newname.txt | Save as |
Stuck? Press Esc to return to Normal mode first, then type the command.
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 (:).
| Keys | Move |
|---|---|
h j k l | Left, down, up, right |
w / b / e | Next word / previous word / end of word |
0 / ^ / $ | Line start / first non-blank / line end |
gg / G / 42G | First line / last line / line 42 |
% | Matching bracket |
| Ctrl+d / Ctrl+u | Half page down / up |
fx / tx | Jump to / just before the next x on the line |
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.
/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.
: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.
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.
Press Esc, then type :q! and Enter to quit without saving, or :wq to save and quit.
Run :set paste before pasting and :set nopaste after, or paste from the system clipboard with "+p if your Vim has clipboard support.