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
| Key | Selects |
|---|---|
v | Characterwise |
V | Linewise |
Ctrl-v | Blockwise (a rectangle) |
gv | Reselect the previous selection |
o | Jump to the other end of the selection |
O | In 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
| Key | Action on the selection |
|---|---|
d or x | Delete |
y | Yank |
c | Change |
p | Replace the selection with the register |
> < | Indent / dedent |
= | Reindent |
u U ~ | Lowercase / uppercase / toggle case |
gq | Rewrap to textwidth |
J | Join 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.
| Key | Action |
|---|---|
Ctrl-v then I | Insert text at the left edge of every line |
Ctrl-v then A | Append text at the right edge of every line |
Ctrl-v then $A | Append at the true end of every line, ragged |
Ctrl-v then c | Change the block |
Ctrl-v then d | Delete the block |
Ctrl-v then r- | Fill the block with - |
Ctrl-v then y | Yank 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}.
| Register | Holds |
|---|---|
"" | The unnamed register: the last yank or delete |
"0 | The last yank only (survives deletes) |
"1 to "9 | The delete ring, "1 is the most recent multi-line delete |
"a to "z | Named registers you control |
"A to "Z | Same 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
| Key | Action |
|---|---|
p | Put after the cursor (or below, if linewise) |
P | Put before the cursor (or above) |
gp gP | Put, then leave the cursor after the new text |
]p | Put linewise, adjusting indent to the current line |
3p | Put the register three times |
:put a | Put register a on a new line below |
:0put a | Put 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.
| Key | Action |
|---|---|
q{reg} | Start recording into a register |
q | Stop recording |
@{reg} | Play the macro |
@@ | Replay the last macro |
3@a | Play macro a three times |
99@a | Play it until it errors out (the usual idiom) |
:normal @a | Run a macro from an Ex command |
:%normal @a | Run it on every line |
:'<,'>normal @a | Run 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$.