Paul Gorman's Vim Cheatsheet

Insert Mode

ESC
Exit insert mode
Ctrl-[
Exit insert mode
i, a
Insert before, after cursor
I, A
Insert start, end of line
gI
Insert start of line, even before whitespace
o, O
Insert new line below, above
gi
Insert at last insert point
C
Change to end of line
S
Substitute line
s
Substitute character
Ctrl-o cmd
Run Normal cmd, resume Insert
Ctrl-n, Ctrl-p
Complete word
Ctrl-x-l
Complete line
Ctrl-d
Deindent
Ctrl-t
Increase indent
Ctrl-r-x
Paste register x contents

Normal Mode

Construct commands like:

["register][operator][count][motion]

Operators

d
Delete/cut
dd
Delete current line
D
Delete to end of line
y
Yank/copy
yy
Yank current line
p, P
Paste before, after cursor
c
Change
u
Undo
Ctrl-r
Redo
r
Replace character
.
Repeat last operation
J
Join with next line
gJ
Join without spaces
U
Undo all changes to line
R
Type over
>>, <<
Indent, deindent
==
Autoindent
~
Change case
ZZ
Save and close
ZQ
Close without saving
mx
Set mark x (a-z)
mX
Set GLOBAL mark X (A-Z)

Motions

h, j, k, l
Char left, down, up, right
0, gm, $
Start, midule, end of line
^
First non-white character of line
b, w
Word left, right
B, W
Whitespaced word left, right
/, ?
Search forward, back (e.g. y/foo)
ge, e
Word end left, right
gE, E
Whitespaced word end left, right
fx, Fx
Next, previous character x
; ,
Repeat last tx or Tx; comma: opposite direction
H, M, L
Top, midule, bottom of screen
Ctrl-f, Ctrl-b
Next, previous screen
Ctrl-d, Ctrl-u
Next, previous half-screen
gg, G
Start, end of file
g;, g,
Previous, next change
``
Last jump
Ctrl-o, Ctrl-i
Older, newer jump
[ENTER]
First non-white char of next line
%
Matching brace
{, }
Next, previous empty line
(, )
Next, previous sentence
nG
Line number n
n|
Column n
]s, [s
Next, last misspelled
x, X
Delete forward, back
*, #
Next, last word under cursor
n, N
Next, previous search result
`x
Jump to mark x

Registers

"a-z
Registers a-z
"0-9
Delete registers 0-9
qx
Record to x
q
Stop recording
@x
Execute x
@@
Repeat last macro
:reg
Show register contents
Ctrl-r-x
Paste x (Insert mode)

Text Objects

daw
Delete around word
cis
Change in sentence
dap
Delete around paragraph
yi"
Yank in quotes
ci{
Change in brackets
cit
Change in tags (HTML)
vib
Visual in block
:h text-objects

Command Mode

:marks
List marks
:ls (or :buffers)
List buffers
:changes
Show changes
:map
Show custom key mappings
:reg
Show register contents
:history
Show command history
:jumps
Show jump list
:10,20 d
Delete line 10 to 20
:'x,'y d
Delete mark x to y
:'<,'>w new.txt
Write selection to new file
/foo
Search foo forward
?foo
Search foo backward
:%s/foo\(\d\+\)/bar\1/gic
Replace all, ignore case, confirm each. Note backreference and escaped parens.
:s/foo/bar/gI
Replace all on current line, mind case
:s/\%Vfoo/bar/g
Match only within visual selection with \%V
&
Repeat last :s
:'x,'y s/foo/bar/g
Replace between marks x and y
:99
Go to line 99

File Commands

:e file
Open file
:enew
New file
:e .
Explore dir
:e sftp://me@example.com/myfile
Open sftp
:e!
Revert to saved
:w file
Save file
:wq
Save, close
:q!
Close, don't save

Options

:set number
Show line numbers
:set rnu
Show relative line numbers
:set paste
Autoindent off
:set hlsearch
Highlight matches
:set syntax=perl
Set syntax
:set noopt
Turn off opt
:set opt?
Show value of opt
:help options
:set
Show current options

Ranges

:help ranges
:5,10d
delete lines 5–10
:*d
delete last VISUAL selection
:.,.+10
delete current and next 10 lines
:/foo/+1d
delete line after next line containing foo
:.,'td
delete current line to mark t

Visual Mode

v
Enter visual mode
V
Enter line-wise visual mode
Ctrl-v
Visual block-wise mode
I, A
Prepend, append to block selection
<, >
Shift block selection (e.g. columns)
ggVG
Select all
=
Auto format selected
gv
Re-select visual area
vip
Select paragraph

Miscellaneous

z=
suggest spelling
zg, zug
Add to dict, undo add
Ctrl-a
Increment number under cursor
Ctrl-x
Decrement number under cursor
ci[)"}]
Delete inside ()/"/{} and insert
:[range]w !{cmd}
Execute cmd with range as stdin, like :w !sbcl or :'<,'>w !python
g Ctrl-g
Word count
g?
Rot 13
:ce 80
Center line columns
:r !date
Insert external command output (e.g. date)
:%! filter
Filter whole file
:| filter
Same as above
:'<,'>! filter
Filter visual selection
:'<,'>! sort
Example of above
:ab lw LongWord
Abbreviate LongWord
:una lw
Unset abbreviation
:ascii
Show ASCII value of char under cursor
:%s/pattern//gn
Count matches
:so ~/.vimrc
Reload .vimrc
:redraw! or Ctrl-l
Clean up the screen
:retab
Apply current tabstop settings

Pipe to vim like:
$ grep 'foo*bar' file.txt | vim -

Buffers and Windows

:bn, :bp
Next, prev buffer
:bd
Close buffer
:sp, :vsp
Split view horizontal, vertical
Ctrl-w Ctrl-w
Move cursor viewport
Ctrl-w q
Close viewport
Ctrl-w [-+=_]
Resize viewport
:bufdo %s/foo/bar
Do to all buffers
zz, zt, zb
View line center, top, bottom

Folding

:set foldmethod=manual
or syntax, indent, etc.
:[range]fold
Create fold on range or selection
zf[move]
Create fold
zo
Open fold
zc
Close fold
zd
Delete this fold
zE
Delete all folds
:help folding

Help

:help
:h help
:h quickref
Quick reference
:h topic
Help on topic
Ctrl-]
Follow help link
Ctrl-t
Back to previous

Command-Line Window

q:
Edit command-mode history
q/
Edit search strings
:h command-line
Ctrl-f
Edit from command mode
Ctrl-c
Abort edit

Ex Mode (g & norm)

:[range]g[lobal]/pattern/[command]
Run the Ex command on all lines in [range] matching pattern
:[9,20]g/^/d A
Delete lines 9–20.
:g/foobar/y A
Yank all lines matching foobar into copy buffer
:g/^Foo/norm 0xxxBar
norm runs commands as if you typed them
:v/pattern/[command]
Like :g but finds NON-matching lines
:%norm @x
Run macro @x on all lines
:5,10norm @x
Run macro @x on lines 5-10

Type ctrl-v ESC to insert an escape keystroke in the norm argument.

OS copy/paste buffers

* is the X11 primary/selection buffer (middle-click/shift-insert). + is the clipboard/cut buffer (ctrl-c/ctrl-v).

"*p
"+y
"*dd
:%y +

Under Windows, the * and + registers are equivalent.

Format Options

Format to 80 columns (see :help formatoptions):
:set textwidth=80
ggVG
gq
{motion} like gqap to format around paragraph