Vim Cheatsheet

Motions and Marks

Use this Vim reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Character and Line Motions

KeyMoves to
h j k lleft, down, up, right
0First column of the line
^First non-blank character
$End of line
g_Last non-blank character
ggFirst line of the file
GLast line of the file
42G or :42Line 42
gj gkDown/up by screen line (wrapped text)
g0 g$Start/end of the screen line
+ -First non-blank of next / previous line
|Column 1; 20| goes to column 20

Word Motions

KeyMoves to
wStart of next word
WStart of next WORD (whitespace-delimited)
bStart of previous word
BStart of previous WORD
eEnd of current or next word
EEnd of current or next WORD
geEnd of previous word

A word stops at punctuation, a WORD only at whitespace. In foo.bar_baz, w walks foo.bar_baz, while W jumps over the whole thing.

Find on the Line

KeyAction
f{char}Jump forward onto the next {char}
F{char}Jump backward onto the previous {char}
t{char}Jump forward until just before {char}
T{char}Jump backward until just after {char}
;Repeat the last f/t in the same direction
,Repeat it in the opposite direction
" Cursor at the start of:  parse(input, opts)
f,      " lands on the comma
dt)     " deletes ' opts' up to but not including ')'
ci(     " change everything inside the parentheses

Screen and Scrolling

KeyAction
H M LTop / middle / bottom line of the screen
zt zz zbScroll so the current line is at top / center / bottom
Ctrl-e Ctrl-yScroll one line down / up
Ctrl-d Ctrl-uScroll half a screen down / up
Ctrl-f Ctrl-bScroll a full screen forward / back

zz is the one to build a habit around: it recenters what you are reading without moving the cursor.

Paragraphs, Sentences, Brackets

KeyMoves to
{ }Previous / next blank line (paragraph)
( )Previous / next sentence
%The matching bracket, paren, or brace
[[ ]]Previous / next section (or top-level {)
[{ ]}Start / end of the enclosing block
[( ])Start / end of the enclosing parens
[m ]mStart of previous / next method (Java-like)

Jumps and Changes

Vim records a jumplist and a changelist so you can retrace your steps.

KeyAction
Ctrl-oGo to the previous position in the jumplist
Ctrl-i or TabGo to the next position
:jumpsShow the jumplist
g;Go to the previous change
g,Go to the next change
:changesShow the changelist
` `Back to the position before the last jump
''Back to the line before the last jump
` . ``To the position of the last change
giInsert mode at the last insert position
gvReselect the last Visual selection

A "jump" is a big move (G, /, %, {). Small moves like j and w do not touch the jumplist, which is what makes Ctrl-o useful.

Marks

KeyAction
m{a-z}Set a mark, local to the buffer
m{A-Z}Set a global mark (works across files)
` {mark} ``Jump to the mark's exact position
'{mark}Jump to the mark's line
:marksList every mark
:delmarks a bDelete marks a and b
:delmarks!Delete all lowercase marks
Automatic markSet to
` [ ` ] ``Start / end of the last change or yank
` < ` > ``Start / end of the last Visual selection
` " ``Position when you last left the file
` ^ ``Position where Insert mode was last stopped
ma       " mark this spot as 'a'
G        " go read the end of the file
`a       " snap back exactly where you were
d'a      " or: delete from here to the line of mark a

Counts and Relative Numbers

Motions take counts, and with relative line numbers on you can read the count straight off the gutter.

:set number relativenumber   " absolute on the cursor line, relative elsewhere
CommandAction
5jDown 5 lines
d7kDelete 7 lines upward
3fxJump to the third x on this line
2}Forward 2 paragraphs
10xDelete 10 characters