Overview
Vim is installed on virtually every Linux and macOS system, which makes it the one editor you can always rely on. The learning curve is steep, but a small set of commands covers the vast majority of daily editing. This cheat sheet organizes them by task.
Vim Modes
| Mode | Purpose | Enter from Normal mode |
| Normal | Navigation and commands | Esc |
| Insert | Typing text | i, a, o |
| Visual | Selecting text | v, V, Ctrl+v |
| Command-line | Save, quit, search and replace | : |
Entering Insert Mode
| Key | Action |
i | Insert before cursor |
a | Insert after cursor |
o | New line below, enter insert mode |
O | New line above |
I | Insert at start of line |
A | Insert at end of line |
Navigation
h j k l left, down, up, right
w / b next / previous word
0 / $ start / end of line
gg / G first / last line of file
:42 jump to line 42
Ctrl+d / Ctrl+u half page down / up
Editing
| Command | Action |
x | Delete character under cursor |
dd | Delete (cut) entire line |
yy | Yank (copy) entire line |
p / P | Paste after / before cursor |
dw | Delete word |
cw | Change word |
u | Undo |
Ctrl+r | Redo |
. | Repeat last change |
Search and Replace
/pattern search forward
?pattern search backward
n / N next / previous match
:%s/old/new/g replace all in file
:%s/old/new/gc replace with confirmation
:5,20s/old/new/g replace only in lines 5-20
Save and Quit
| Command | Action |
:w | Save |
:q | Quit |
:wq or ZZ | Save and quit |
:q! | Quit without saving |
:w !sudo tee % | Save a file opened without sudo |
Working with Multiple Files
:e filename open a file
:bn / :bp next / previous buffer
:sp filename horizontal split
:vsp filename vertical split
Ctrl+w h/j/k/l move between splits
:tabnew open new tab
Macros
qa start recording into register a
... perform edits
q stop recording
@a replay macro
10@a replay 10 times