Learn Practice Daily golf Reference

How to use registers in Vim

Put a double quote and a letter in front of a yank or a paste. "ayy copies the line into register a, and "ap pastes it. Plain yanks and deletes leave the letter registers alone, so your copy stays there until you replace it.

The ninja pulling a glowing line of code out of a drawer marked a in a wall of little lettered spice drawers, with keycaps for ", a and p on the counter.

Three new dishes go on the menu tomorrow. The mole burrito and the chorizo burrito need the hot note that's under the al pastor, and the squash tacos need the vegan note from the bean bowl. With plain yy and p you'd carry one note at a time, because each yank throws out the one before. Put each note in a register of its own, named by a letter, and you can carry both.

Before
Al pastor burrito
Hot: habanero salsa
Bean bowl
Vegan: no cheese, no crema
Mole burrito
Squash tacos
Chorizo burrito
"ayyjjj"ap
Al pastor burrito
Hot: habanero salsa
Bean bowl
Vegan: no cheese, no crema
Mole burrito
Hot: habanero salsa
Squash tacos
Chorizo burrito
kk"byyjjj"bp
Al pastor burrito
Hot: habanero salsa
Bean bowl
Vegan: no cheese, no crema
Mole burrito
Hot: habanero salsa
Squash tacos
Vegan: no cheese, no crema
Chorizo burrito
j"ap
Al pastor burrito
Hot: habanero salsa
Bean bowl
Vegan: no cheese, no crema
Mole burrito
Hot: habanero salsa
Squash tacos
Vegan: no cheese, no crema
Chorizo burrito
Hot: habanero salsa
The hot note goes into register a and the vegan note into b. The last "ap comes after "byy and still pastes the hot note
Try it here

Notes for the new dishes

The cursor is on the hot note.

Copy the hot note into register a with "ayy. Go down to the mole burrito with j j j and paste the note under it with "apmenu.txt
1Al pastor burrito
2 Hot: habanero salsa
1Bean bowl
2 Vegan: no cheese, no crema
3Mole burrito
4Squash tacos
5Chorizo burrito
NORMALLoading the editor…0.0sLn 2, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Add to a register with a capital letter

A lowercase letter replaces what the register held. The same letter in capitals adds to it: "Ayy puts the line after whatever register a already holds. That lets you collect lines from all over a file and paste them in one go. Any letter from a to z will do, so pick one you'll remember, like h for hot.

Try it here

The hot list

The board needs a list of the hot dishes. The cursor is on the first one.

Copy the tacos into register h with "hyy. Two lines down with j j, add the Diablo with "Hyy. Then j j to the last line and paste both with "hpboard.txt
1Carnitas burrito
2Habanero tacos
1Bean bowl
2Diablo burrito
3Cheese quesadilla
4Hot today:
NORMALLoading the editor…0.0sLn 2, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

To check what your registers hold before you paste, type :reg (short for :registers) and press ↵. It lists every register that holds something, with ^J marking the line breaks.

Get back what you yanked or deleted earlier

Vim fills some registers on its own. Register 0 holds your last yank, and only a yank changes it, so "0p pastes what you copied even after you've deleted something since. Paste without overwriting has the whole trick.

Each time you delete a line or more, it goes into register 1, and what was in 1 moves along to 2, 2 to 3 and so on up to 9. So "1p pastes the last line you deleted, and "2p the one before it. A smaller delete, such as a word with dw, goes to register - instead.

Check yourself

Two specials gone

Yesterday's two specials are the top two lines, and the cursor is on the first. In Normal mode you delete both with dd dd, then press "2p. Which line comes back?

BeforeJust a picture: pick an answer below (1, 2, 3)
1Pumpkin burrito
1Mole tacos
2Carnitas burrito
3Bean bowl
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

"_dd deletes the line into _, the black hole, and fills no register at all. Register + is your computer's clipboard, which Copy to the system clipboard sets up.

Paste a register while you type

In Insert mode, Ctrl-r followed by a register's name types out what it holds, and you carry on typing. It saves you from spelling a long or awkward word twice. Copy the word into a register with "ayiw (which copies the word under the cursor), then Ctrl-ra wherever you need it.

Try it here

Spell it once

Nobody at the shop can spell the new special. The cursor is at the start of its name.

Copy the name into register a with "ayiw. Go down with j and add to the end of the line with A: a space, Ctrl-ra, then today! and Escnotes.txt
1Special: huitlacoche quesadilla
1Board: Ask about our
2Kitchen: order more
NORMALLoading the editor…0.0sLn 1, Col :10
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
"ayy / "ayiwCopy the line / the word into register a (any letter from a to z).
"ap / "aPPaste register a after / before the cursor.
"AyyAdd the line to the end of register a.
"addCut the line into register a.
"0pPaste your last yank, whatever you've deleted since.
"1p to "9pPaste a line you deleted, the newest in register 1.
"1p, then .Paste the last deleted line, then each . pastes the one deleted before it.
"-pPaste your last delete inside a line, such as a word.
"_ddDelete without changing any register (the black hole).
"+y / "+pCopy to / paste from the system clipboard.
Ctrl-raIn Insert mode: type out what register a holds.
:reg ↵List every register that holds something.
Practise the keys

From memory

Which keys do it?
Copy the current line into register b, where the next yank or delete leaves it alone.
scroll here, then press it
1 of 4
Copy the current line into register b, where the next yank or delete leaves it alone.