Vim Cheatsheet

Visual Mode, Registers, and Macros

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

Visual Modes

KeySelects
vCharacterwise
VLinewise
Ctrl-vBlockwise (a rectangle)
gvReselect the previous selection
oJump to the other end of the selection
OIn block mode, jump to the other corner

Once something is selected, any operator applies to it: d, y, c, >, gU, =, gq, !.

Operating on a Selection

KeyAction on the selection
d or xDelete
yYank
cChange
pReplace the selection with the register
> <Indent / dedent
=Reindent
u U ~Lowercase / uppercase / toggle case
gqRewrap to textwidth
JJoin into one line
r{char}Replace every selected character
:Start an Ex command scoped to the range

Pressing : in Visual mode pre-fills :'<,'> so the command applies only to the selection.

V3j:s/foo/bar/g<Enter>      " substitute in 4 lines
vip:!sort<Enter>            " sort the paragraph
V}gq                        " rewrap to the end of the paragraph

Text Objects in Visual Mode

Text objects extend the selection instead of being consumed by an operator, so you can grow a selection step by step.

vi"      " select inside the quotes
va"      " select including the quotes
vip      " select the paragraph
vit      " select inside the HTML tag
viwiw    " select a word, then extend by another
vi{V     " select inside braces, then switch to linewise

Blockwise Visual Editing

Blockwise (Ctrl-v) selects a rectangle, which is how you edit columns.

KeyAction
Ctrl-v then IInsert text at the left edge of every line
Ctrl-v then AAppend text at the right edge of every line
Ctrl-v then $AAppend at the true end of every line, ragged
Ctrl-v then cChange the block
Ctrl-v then dDelete the block
Ctrl-v then r-Fill the block with -
Ctrl-v then yYank the block, pastes back as a block
" Comment out five lines of Python
Ctrl-v 4j I # <Esc>

" Add a trailing comma to a ragged list of lines
Ctrl-v 4j $ A , <Esc>

" Delete the first two characters of ten lines
Ctrl-v 9j l d

The trick with I and A is that the change is typed once and applied to every line in the block when you press Esc.

Registers

A register is a named clipboard. Prefix any yank, delete, or put with "{reg}.

RegisterHolds
""The unnamed register: the last yank or delete
"0The last yank only (survives deletes)
"1 to "9The delete ring, "1 is the most recent multi-line delete
"a to "zNamed registers you control
"A to "ZSame registers, but append instead of overwrite
"-The last small (less than one line) delete
"_The black hole: writes go nowhere
"+The system clipboard
"*The X11 primary selection (middle-click)
"%The current filename (read-only)
"#The alternate filename (read-only)
".The last inserted text (read-only)
":The last Ex command (read-only)
"/The last search pattern
"=The expression register
"ayy       " yank this line into register a
"Ayy       " append the next line to register a
"ap        " put register a
"+y5j      " yank 6 lines to the system clipboard
"+p        " paste from the system clipboard
"_dd       " delete a line without clobbering the unnamed register
:reg       " show every register
:reg a b   " show just registers a and b

The "0 register is the fix for the classic frustration: yank, delete something, then "0p still pastes the yank rather than the delete.

Put in Detail

KeyAction
pPut after the cursor (or below, if linewise)
PPut before the cursor (or above)
gp gPPut, then leave the cursor after the new text
]pPut linewise, adjusting indent to the current line
3pPut the register three times
:put aPut register a on a new line below
:0put aPut register a at the top of the file

In Visual mode, p overwrites the selection with the register. That is the fastest "replace this with what I copied", though note it swaps the register contents.

Macros

A macro is just a register full of keystrokes.

KeyAction
q{reg}Start recording into a register
qStop recording
@{reg}Play the macro
@@Replay the last macro
3@aPlay macro a three times
99@aPlay it until it errors out (the usual idiom)
:normal @aRun a macro from an Ex command
:%normal @aRun it on every line
:'<,'>normal @aRun it on every selected line
" Turn each line "name,email" into "name <email>"
qa                 " record into a
0f,ci,<Space><     " replace the comma
A><Esc>            " close the angle bracket
j                  " move to the next line, so the macro is repeatable
q                  " stop
99@a               " apply to the rest of the file

Two rules make macros reliable: start from a known position (0 or ^), and end by moving to the next target so the macro can be repeated with a count. A macro that errors out stops the count, which is why 99@a is safe.

Because a macro lives in a register, you can edit it as text: paste it with "ap, fix the keystrokes, then yank it back with "ay$.