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

ModePurposeEnter from Normal mode
NormalNavigation and commandsEsc
InsertTyping texti, a, o
VisualSelecting textv, V, Ctrl+v
Command-lineSave, quit, search and replace:

Entering Insert Mode

KeyAction
iInsert before cursor
aInsert after cursor
oNew line below, enter insert mode
ONew line above
IInsert at start of line
AInsert 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

CommandAction
xDelete character under cursor
ddDelete (cut) entire line
yyYank (copy) entire line
p / PPaste after / before cursor
dwDelete word
cwChange word
uUndo
Ctrl+rRedo
.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

CommandAction
:wSave
:qQuit
:wq or ZZSave 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