DaleSchool

Registers and the Clipboard

Beginner15min

Learning Objectives

  • Explain the roles of the default and numbered registers
  • Use named registers
  • Integrate Vim with the system clipboard

Working Code

Open this code in Vim:

const firstName = "Alice";
const lastName = "Kim";
const fullName = firstName + " " + lastName;
  1. On the first line, press yy to yank the line.
  2. On the second line, press dd to delete the line.
  3. Press p — you paste the line you deleted with dd, not the one you yanked.

Why? Because dd overwrote the default register. In Vim, delete also saves to a register. To fix this, you need to understand registers.

Try It Yourself

Using the same code, try a named register this time:

const firstName = "Alice";
const lastName = "Kim";
const fullName = firstName + " " + lastName;
  1. On the first line, press "ayy — yank the line into register a.
  2. On the second line, press dd — delete the line (goes into the default register).
  3. Press "ap — paste from register a. The first line is pasted correctly.

"a is a prefix meaning "use register a." It's like putting a label on a drawer.

"Why?"

Registers are drawers

A regular editor has only one clipboard. Copy something new, and the previous content vanishes. Vim has many drawers (registers) so you can hold multiple pieces of text at once.

Types of registers

RegisterNameDescription
""Default registerStores results of d, c, y, etc.
"0Yank registerStores only y results
"1~"9Numbered registersEach delete pushes down to older slots
"a~"zNamed registersUser-assigned
"+System clipboardShares with the OS clipboard
"_Black hole registerDelete without saving (true delete)

Default register ("") vs. yank register ("0)

This distinction matters:

yy  → stored in both "" and "0
dd  → stored in "" only (doesn't touch "0)

So if you yank with yy, then delete another line with dd, you can still paste the yanked content with "0p. Register "0 remembers only yanked (y) content.

Using named registers

You have 26 named registers from "a to "z:

"ayy    → yank the current line into register a
"bdd    → delete (store) the current line into register b
"ap     → paste from register a
"bp     → paste from register b

Use an uppercase letter to append to the existing content:

"ayy    → store the first line in register a
"Ayy    → append the second line to register a

Inspect registers

Run :registers (or :reg) to see the contents of all registers:

:reg        " show all registers
:reg a      " show only register a
:reg ab0    " show registers a, b, and 0

System clipboard integration

To move text between Vim and other apps, use the "+ register:

"+y     → yank to system clipboard (Vim → browser)
"+p     → paste from system clipboard (browser → Vim)
"+yy    → yank the current line to system clipboard

Use it to paste code from a browser into Vim, or from Vim into Slack.

Black hole register ("_)

Delete something without polluting registers:

"_dd    → truly delete the current line (saved nowhere)
"_diw   → truly delete a word

Useful when you want to preserve a previously yanked value while removing unwanted text.

Deep Dive

Special registers

Vim also has read-only special registers:

RegisterContent
"%Current file name
"#Alternate file name
".Last inserted text
":Last executed command
"/Last search pattern

For example, "%p pastes the name of the current file.

Rearrange code with registers

Reorder the lines in this code using registers:

third = "C"
first = "A"
second = "B"

Target order:

first = "A"
second = "B"
third = "C"

How:

  1. On the first line, press "ayy to yank into register a.
  2. On second, press "byy to yank into register b.
  3. On third, press "cyy to yank into register c.
  4. Delete all three lines (3dd).
  5. Paste in the desired order: "ap, "bp, "cp.

Challenge: copy the result to the system clipboard with "+y and paste it into another application.

question: "You yanked with yy, then deleted a line with dd. How do you paste the originally yanked content?" options:

question: "Which register lets you paste Vim-yanked text into a browser?" options:

question: "How do you delete text without polluting any register?" options: