Registers: The Power of Multiple Clipboards
[!NOTE] The Clipboard Problem In standard editors, you have one clipboard. If you copy
A, then copyB,Ais gone forever. In Vim, you have 48+ registers (clipboards). You never lose text unless you want to.
1. Understanding Registers
A register is simply a named storage location for text. When you yank (copy) or delete (cut) text, Vim stores it in a register.
To specify a register, you type " followed by the register name, before the command.
"ayw→ Yank word into registera."ap→ Paste from registera.
If you don’t specify a register, Vim uses the Unnamed Register ("").
2. The Vital Registers
You don’t need to memorize all of them, but these are essential for daily work.
1. The Unnamed Register ("")
The default. d, c, s, x, y all put text here. p pastes from here by default.
2. The Yank Register ("0)
This is the most important register to learn.
When you yank (y) text, it goes into "" AND "0.
When you delete (d) text, it goes into "" but NOT "0.
Use Case: You yanked a line, then deleted something else. Your yank is gone from "", but it is safe in "0. Use "0p to paste it.
3. The Numbered Registers ("1 - "9)
Vim remembers your last 9 deletes (of more than one line).
"1: Most recent delete."2: Second most recent delete.- …and so on. Think of this as a “Clipboard History” for deleted text.
4. The Named Registers ("a - "z)
These are yours to control. Store text here for long-term use within an editing session.
- Lowercase (
"a): Overwrites the register. - Uppercase (
"A): Appends to the register.
5. The System Clipboard ("+ or "*)
This connects Vim to your OS (Mac/Windows/Linux) clipboard.
"+y→ Copy to system clipboard (so you can Cmd+V in a browser)."+p→ Paste from system clipboard.
6. The Black Hole Register ("_)
Writing to this register does nothing. Use it to delete text without overwriting your current clipboard.
"_dd→ Delete line, but keep""unchanged.
Interactive: Register Dashboard
Perform actions and watch how the registers update. The text below is your buffer.
3. Accessing Registers
Viewing Content
Use :reg to see the contents of all registers.
Use :reg a to see just register a.
Pasting in Insert Mode
You are typing in Insert mode and realize you need to paste text from register a.
Do not press <Esc>.
Type Ctrl-r followed by a.
The text appears instantly.
[!TIP] Calculations in Insert Mode The Expression Register (
"=) allows you to do math. In Insert Mode, typeCtrl-rthen=. You will see=at the bottom. Type25*4and press<Enter>.100will be inserted into your text.
4. Cheat Sheet
| Command | Description |
|---|---|
"" |
Unnamed register (default). |
"0 |
Yank register (most recent copy). |
"1-"9 |
Delete history. |
"a-"z |
Named registers. |
"A |
Append to named register a. |
"+ |
System clipboard (Copy/Paste to OS). |
"_ |
Black hole (Delete without saving). |
:reg |
View all registers. |
<C-r> {reg} |
Paste register in Insert mode. |
5. Practice Exercise
- Type 3 lines of text.
- Delete line 1 (
dd). Check:reg(it’s in""and"1). - Yank line 2 (
yy). Check:reg(it’s in""and"0). - Paste the deleted line 1. (Hint:
""now has the yanked text, sopis wrong. Use"1p).