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
| Key | Where it starts typing |
|---|---|
i | Before the cursor |
a | After the cursor |
I | At the first non-blank of the line |
A | At the end of the line |
o | On a new line below |
O | On a new line above |
gI | At column 1, ignoring indent |
gi | Where you last left Insert mode |
s | Delete the character, then insert |
S or cc | Clear the line, then insert |
C | Clear 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.
| Operator | Action |
|---|---|
d | Delete (into a register) |
c | Change: delete, then Insert mode |
y | Yank (copy) |
p P | Put after / before the cursor |
> < | Indent / dedent |
= | Auto-indent |
gu gU | Lowercase / uppercase |
g~ | Toggle case |
gq | Reformat to textwidth |
g? | ROT13 (yes, really) |
! | Filter through an external command |
zf | Create 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).
| Object | Selects |
|---|---|
iw / aw | Word / word plus trailing whitespace |
iW / aW | WORD / WORD plus whitespace |
is / as | Sentence |
ip / ap | Paragraph |
i" / a" | Inside double quotes / including them |
i' / a' | Single quotes |
` i ` / a `` | Backticks |
i( ib / a( ab | Parentheses |
i{ iB / a{ aB | Braces |
i[ / a[ | Brackets |
i< / a< | Angle brackets |
it / at | HTML/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
| Key | Action |
|---|---|
x | Delete the character under the cursor |
X | Delete the character before the cursor |
r{char} | Replace one character |
R | Replace mode (overtype until Esc) |
~ | Toggle case of one character and advance |
J | Join this line with the next |
gJ | Join without inserting a space |
Ctrl-a | Increment the number under the cursor |
Ctrl-x | Decrement it |
g Ctrl-a | In 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.
| Key | Action |
|---|---|
Ctrl-w | Delete the word before the cursor |
Ctrl-u | Delete to the start of the line |
Ctrl-h | Backspace |
Ctrl-t Ctrl-d | Indent / 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-p | Complete the word from the buffer |
Ctrl-x Ctrl-f | Complete a filename |
Ctrl-x Ctrl-l | Complete a whole line |
Ctrl-x Ctrl-o | Omni 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.