Overview
A register is Vim’s internal clipboard.
Unlike a normal editor that has one clipboard, Vim has many registers that can store:
- Yanked (copied) text
- Deleted text
- Recorded macros
- Search patterns
- Commands
- File names
- Expressions
Think of them as named storage locations.
View Registers
Show all non-empty registers:
:regor
:registersShow specific registers:
:reg a
:reg a b cEmpty registers are not displayed.
Unnamed Register (")
The default register.
Commands like:
yy
dd
xstore text into the unnamed register.
Paste normally:
p
Puses this register.
Named Registers (a-z)
Store text into a register:
"ayyPaste:
"apExample:
Line 1
Line 2Copy line 1:
"ayyMove elsewhere:
"apAppend to a Register
Lowercase replaces:
"ayyUppercase appends:
"AyyUseful for collecting multiple snippets.
Numbered Registers
| Register | Purpose |
|---|---|
0 | Last yank |
1 | Last delete |
2-9 | Older deletes |
Example:
yy
ddNow:
ppastes deleted text.
While
"0pstill pastes the last yanked text.
Black Hole Register (_)
Delete without changing any register.
"_ddUseful when you want to preserve your previous yank.
System Clipboard
If a clipboard provider exists:
Copy:
"+yPaste:
"+pOn remote SSH servers this usually does not work unless clipboard forwarding (e.g. OSC52) is configured.
Insert Register Contents
While in Insert mode:
<C-r>aInsert contents of register a.
Insert last yank:
<C-r>0Insert unnamed register:
<C-r>"Very useful while typing.
Expression Register
Evaluate expressions.
In Insert mode:
<C-r>=2+3Press Enter.
Result:
5Macro Registers
Start recording into register a:
qaPerform edits.
Stop:
qReplay:
@aRepeat last macro:
@@Macros are simply registers containing keystrokes.
Useful Read-only Registers
| Register | Meaning |
|---|---|
% | Current file name |
: | Last command |
/ | Last search |
. | Last inserted text |
| Example: |
:echo @%Or insert current filename while typing:
<C-r>%Practical Commands
| Command | Description |
|---|---|
:reg | Show registers |
"ayy | Yank into register a |
"ap | Paste from register a |
"Ayy | Append to register a |
"0p | Paste last yank |
"_dd | Delete without overwriting registers |
"+y | Copy to system clipboard |
<C-r>a | Insert register a |
@a | Run macro in register a |
Recommended Daily Usage
These are the commands most worth memorizing:
:reg"ayy"ap"0p"_dd<C-r>"Once these become muscle memory, registers become one of Vim’s most powerful editing features.
Practice
Create a file with several lines.
- Copy line 1 into register
a.
"ayy- Copy line 2 into register
b.
"byy- Paste both.
"ap
"bp- Delete a line without losing the copied text.
"_dd- Paste the last yank.
"0p- Inspect the registers.
:regYou should now see registers a, b, 0, and the unnamed register populated.