Working Code
Open this code in Vim:
const firstName = "Alice";
const lastName = "Kim";
const fullName = firstName + " " + lastName;
- On the first line, press
yyto yank the line. - On the second line, press
ddto delete the line. - Press
p— you paste the line you deleted withdd, 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;
- On the first line, press
"ayy— yank the line into registera. - On the second line, press
dd— delete the line (goes into the default register). - Press
"ap— paste from registera. 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
| Register | Name | Description |
|---|---|---|
"" | Default register | Stores results of d, c, y, etc. |
"0 | Yank register | Stores only y results |
"1~"9 | Numbered registers | Each delete pushes down to older slots |
"a~"z | Named registers | User-assigned |
"+ | System clipboard | Shares with the OS clipboard |
"_ | Black hole register | Delete 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:
| Register | Content |
|---|---|
"% | 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:
- On the
firstline, press"ayyto yank into register a. - On
second, press"byyto yank into register b. - On
third, press"cyyto yank into register c. - Delete all three lines (
3dd). - 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: