Registers: The Power of Multiple Clipboards

[!NOTE] The Clipboard Problem In standard editors, you have one clipboard. If you copy A, then copy B, A is 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 register a.
  • "ap → Paste from register a.

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.

Line 1: Function Definition
Line 2: Variable Declaration
Line 3: Return Statement
Register Content
"" (Unnamed) -
"0 (Yank) -
"1 (Delete) -
"a (Named) -

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, type Ctrl-r then =. You will see = at the bottom. Type 25*4 and press <Enter>. 100 will 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

  1. Type 3 lines of text.
  2. Delete line 1 (dd). Check :reg (it’s in "" and "1).
  3. Yank line 2 (yy). Check :reg (it’s in "" and "0).
  4. Paste the deleted line 1. (Hint: "" now has the yanked text, so p is wrong. Use "1p).