Vim Cheatsheet

Operators and Text Objects

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

Entering Insert Mode

KeyWhere it starts typing
iBefore the cursor
aAfter the cursor
IAt the first non-blank of the line
AAt the end of the line
oOn a new line below
OOn a new line above
gIAt column 1, ignoring indent
giWhere you last left Insert mode
sDelete the character, then insert
S or ccClear the line, then insert
CClear to end of line, then insert

Counts work here too: 3ohello + Esc inserts three lines of "hello".

Operators

An operator waits for a motion or text object.

OperatorAction
dDelete (into a register)
cChange: delete, then Insert mode
yYank (copy)
p PPut after / before the cursor
> <Indent / dedent
=Auto-indent
gu gULowercase / uppercase
g~Toggle case
gqReformat to textwidth
g?ROT13 (yes, really)
!Filter through an external command
zfCreate a fold
gUiw     " uppercase the word under the cursor
=i{      " reindent everything inside the braces
gqip     " rewrap the current paragraph
>3j      " indent this line and the 3 below
!ipsort  " sort the current paragraph through sort(1)

Text Objects

Text objects are the reason ci" beats selecting quotes by hand. i means inner (contents only), a means a (contents plus the delimiters or trailing space).

ObjectSelects
iw / awWord / word plus trailing whitespace
iW / aWWORD / WORD plus whitespace
is / asSentence
ip / apParagraph
i" / a"Inside double quotes / including them
i' / a'Single quotes
` i ` / a ``Backticks
i( ib / a( abParentheses
i{ iB / a{ aBBraces
i[ / a[Brackets
i< / a<Angle brackets
it / atHTML/XML tag contents / whole tag
" Cursor anywhere inside:  greet("world", 3)
ci"    →  greet("|", 3)          change the string
da,    " (with a plugin) delete an argument
ci(    →  greet(|)               change all arguments
dat    " on <p>hi</p>: delete the entire tag

Quote and bracket objects work from anywhere inside the pair, so you do not need to move to the delimiter first.

Small Edits Without Insert Mode

KeyAction
xDelete the character under the cursor
XDelete the character before the cursor
r{char}Replace one character
RReplace mode (overtype until Esc)
~Toggle case of one character and advance
JJoin this line with the next
gJJoin without inserting a space
Ctrl-aIncrement the number under the cursor
Ctrl-xDecrement it
g Ctrl-aIn Visual mode, make an incrementing sequence

Indent and Format

>>            " indent the current line
<<            " dedent it
3>>           " indent 3 lines
=G            " reindent from here to end of file
gg=G          " reindent the whole file
:set shiftwidth=2 expandtab
:'<,'>>       " indent the last visual selection
:set textwidth=80
gqq           " rewrap the current line to textwidth

Insert-Mode Shortcuts

You do not have to leave Insert mode for everything.

KeyAction
Ctrl-wDelete the word before the cursor
Ctrl-uDelete to the start of the line
Ctrl-hBackspace
Ctrl-t Ctrl-dIndent / dedent the line
Ctrl-r{reg}Insert the contents of a register
Ctrl-r=Insert the result of an expression
Ctrl-o{cmd}Run one Normal-mode command, then come back
Ctrl-n Ctrl-pComplete the word from the buffer
Ctrl-x Ctrl-fComplete a filename
Ctrl-x Ctrl-lComplete a whole line
Ctrl-x Ctrl-oOmni completion (language aware)
Ctrl-v{code}Insert a character literally, e.g. Ctrl-v u00e9
Ctrl-k{digraph}Insert a digraph, e.g. Ctrl-k a: gives ä
" In Insert mode
Ctrl-r "     " paste the unnamed register
Ctrl-r %     " insert the current filename
Ctrl-r =2*21 " insert 42
Ctrl-o zz    " recenter without leaving Insert mode

Reading and Filtering

:r file.txt         " insert the contents of a file below the cursor
:r !date            " insert the output of a shell command
:0r template.txt    " insert at the very top
:%!sort             " sort the entire buffer through sort(1)
:%!jq .             " reformat JSON through jq
:.!tr a-z A-Z       " uppercase just this line
:'<,'>!sort -u      " sort and dedupe the selection

! hands lines to an external program and replaces them with its output, which turns any command line tool into a Vim command.