Registers#
Registers are Vim’s storage slots. Every yank, delete, and change goes into a register. Understanding them means you never lose text you’ve cut and gives you access to multiple “clipboards.”
Viewing registers#
:registers
" or shorthand
:regTypes of registers#
| Register | Name | Purpose |
|---|---|---|
"" | Unnamed | Default — last delete, change, or yank |
"0 | Yank | Last yank only (not affected by deletes) |
"1–"9 | Numbered | History of deletes (1 = most recent) |
"a–"z | Named | Your own storage (you explicitly choose these) |
"+ | System clipboard | Copy/paste with other applications |
"* | Selection clipboard | X11 primary selection (middle-click paste) |
"_ | Black hole | Delete without affecting any register |
"/ | Search | Last search pattern |
". | Last insert | Text from last Insert mode session |
"% | Filename | Current file name |
": | Command | Last command-line command |
Using named registers#
Prefix any yank or delete with "x to target register x:
"ayy Yank current line into register a
"bdiw Delete word into register b
"ap Paste from register a
"bp Paste from register bThis gives you multiple independent clipboards.
The yank register ("0)#
The key insight: deletes go into "" and "1, but yanks also go into "0.
So if you yank something, then delete text, your yanked content is safe in "0:
yiw Yank a word
dw Delete another word (overwrites unnamed register)
"0p Paste the original yanked word (still in register 0)System clipboard#
"+y Yank to system clipboard
"+p Paste from system clipboardOr with set clipboard=unnamedplus in your .vimrc, regular y and p use the system clipboard.
The black hole register#
Delete text without saving it anywhere:
"_dd Delete line, don't touch registers
"_diw Delete word, don't touch registersUseful when you want to delete something without overwriting what you previously yanked.
Appending to registers#
Use an uppercase letter to append instead of overwrite:
"ayy Yank line into register a
...move somewhere else...
"Ayy Append this line to register a
"ap Paste both linesPaste in Insert mode#
Ctrl-r a Paste register a while in Insert mode
Ctrl-r " Paste unnamed register in Insert mode
Ctrl-r + Paste system clipboard in Insert mode
Ctrl-r / Paste last search pattern in Insert modeMacros#
Macros record a sequence of keystrokes and replay them. They’re the fastest way to automate repetitive edits.
Recording a macro#
qa Start recording into register a
...perform your edits...
q Stop recordingPlaying a macro#
@a Play macro in register a
@@ Replay last played macro
5@a Play macro a five timesMacro workflow#
The key to a good macro: make it repeatable. End the macro positioned for the next repetition.
Example — add quotes around the first word on each line:
qa Start recording into register a
0 Go to beginning of line
wi" Go to end of word, insert a quote
Esc Back to Normal mode
bi" Go to beginning of word, insert a quote
Esc Back to Normal mode
j Move to next line
q Stop recording
5@a Run on the next 5 linesRunning macros on visual selections#
Select lines with V, then:
:'<,'>normal @aThis applies macro a to each selected line.
Running macros across all lines matching a pattern#
:g/pattern/normal @aEditing a macro#
Macros are just registers. To edit one:
" Paste register a into the buffer
"ap
" Edit the keystrokes as text
" Yank it back into register a
0"ay$
" Delete the pasted line
ddRecursive macros#
A macro can call itself for unlimited repetition:
qa Start recording
...your edits...
j Move to next line
@a Call itself
q Stop recording
@a Start it (runs until it hits an error or end of file)The macro stops when any command fails (like j at the last line).
The Dot Command Revisited#
The . command repeats your last change. It’s simpler than macros for one-off repetition:
ciw replacement Esc Change a word
n Jump to next search match
. Repeat the change
n Next match
. Repeat againWhen to use dot vs macros#
| Scenario | Use |
|---|---|
| Same edit, scattered locations | . with manual navigation |
| Same edit, sequential lines | Macro with j at the end |
| Complex multi-step edit, repeated | Macro |
| Simple single change, repeated | . |
Visual Block Mode#
Ctrl-v enters visual block mode for column-wise selection.
Insert text on multiple lines#
Ctrl-v Enter block mode
jjj Select 4 lines
I Insert before block
// Type the text
Esc Apply to all selected linesAppend text on multiple lines#
Ctrl-v Enter block mode
jjj Select lines
$ Extend to end of each line
A Append after block
; Type the text
Esc Apply to all linesDelete a column#
Ctrl-v Enter block mode
jjj Select rows
ll Extend selection right
d Delete the blockReplace a column#
Ctrl-v Enter block mode
jjj Select rows
lll Select column width
c Change block
newtext Type replacement
Esc Apply to all rowsPower Commands#
Shell commands#
:!ls " Run a shell command
:r !date " Insert command output into buffer
:r !curl -s url " Insert curl output
:w !bash " Send buffer contents to bash
:%!sort " Sort the entire file in place
:%!jq . " Format JSON through jq
:'<,'>!sort " Sort selected linesThe expression register#
Ctrl-r = In Insert mode, evaluate an expression
Type: 2+2 Enter → inserts "4"
Type: system('date') Enter → inserts current dateOther useful commands#
| Command | Action |
|---|---|
J | Join current line with next (adds space) |
gJ | Join without adding space |
~ | Toggle case of character |
g~iw | Toggle case of word |
gUiw | Uppercase word |
guiw | Lowercase word |
Ctrl-a | Increment number under cursor |
Ctrl-x | Decrement number under cursor |
ga | Show ASCII value of character |
gf | Open file path under cursor |
gx | Open URL under cursor |
Practical Examples#
Add a prefix to multiple lines#
Ctrl-v Block select
4j Select 5 lines
I Insert mode
- [ ] Type the prefix
Esc ApplyRecord a macro to format CSV as markdown table#
qa
0 Go to start of line
:s/,/ | /g Enter Replace commas with pipes
I| Esc Add leading pipe
A |Esc Add trailing pipe
j Move to next line
q Stop recording
10@a Apply to next 10 linesIncrement a sequence of numbers#
# Start with:
item1
item1
item1
# Select lines 2-3 with visual block on the number
Ctrl-v jj
g Ctrl-a Increment as a sequence → item1, item2, item3Delete every other line#
:g/pattern/normal ddOr with a macro:
qa jdd q Record: skip a line, delete next
100@a Run 100 timesBest Practices#
- Use the yank register (
"0) when you need to paste something after deleting other text - Use named registers (
"a–"z) when juggling multiple pieces of text - End macros with a movement to the next target so they chain cleanly
- Test macros on one line first before running them with a count
- Prefer the dot command over macros for simple, one-step edits
- Use visual block mode for column operations instead of writing complex macros
- Use
:g/pattern/normal @ato apply macros selectively instead of blindly across the whole file

