Registers: named, yank and system clipboard

Vim · Advanced · 10 min

Registers let you keep several clipboards, recover overwritten yanks and exchange text with other apps.

Every yank and delete goes into a register. Prefix a command with " and a register name to choose which: "ayy yanks a line into register a, "ap puts it.

The important registers:

  • "" the unnamed register, used by plain p. Every yank and delete writes it.
  • "0 holds the last yank only. Deletes do not touch it, so after yiw then dd, "0p still pastes the yanked word.
  • "1..."9 hold recent linewise or multi-line deletes, shifting down each time; small deletes go to "-.
  • "a..."z are yours. An uppercase name appends: "Ayy adds the line to a.
  • "+ is the system clipboard and "* the primary selection on X11 (both need Vim built with +clipboard; check :version).
  • "_ is the black hole: "_dd deletes without clobbering anything.
  • Read-only: ". last inserted text, "% file name, ": last command line, "/ last search.

In Insert mode and on the command line, Ctrl-r followed by a register name inserts its contents: Ctrl-r 0 pastes the last yank, Ctrl-r / the last search pattern. :reg (or :reg a 0) shows what is stored.

Macros, covered next, are stored in these same registers, which means you can edit a macro as text.

Example

"ayiw       " yank word into register a
"Ayiw       " append another word to a
"ap         " put register a
yiw         " yank a word...
dd          " ...then delete a line (unnamed register now holds the line)
"0p         " still pastes the yanked word
"_dd        " delete without touching any register
"+yy        " copy line to the system clipboard
:reg a 0 +  " inspect registers

Remember

  • "0 = last yank, "1-"9 = recent deletes, "a-"z = yours
  • Uppercase register name appends instead of overwriting
  • "+ is the system clipboard; "_ discards

Watch out for

  • Expecting "+ to work in a Vim built without clipboard support; check :version for +clipboard.

Check yourself

  1. You yanked a word, then deleted a line with dd. Which command pastes the word?
  2. What does `"Bdd` do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/change.txt.html#registers