Subscribe for free with iTunes, Twitter, or RSS (Ogg or Quicktime).

Vimcasts

Meet the yank register

#52

Vim’s default register is not a safe place to keep yanked text that you want to paste later. It’s all too easy to clobber the default register with a d or x command. Luckily, the last yanked text is kept safe in Vim’s yank register.

Download

OGG 5.2 MB

Quicktime 9 MB

From Vim’s documentation (:help quote0):

Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with [“x].

I call “numbered register 0” the yank register. You can paste from the yank register with the command: "0p. That comes in really handy when the text you want to paste is no longer present in the default register.

A note on Vim's jargon

Vim uses the terms delete, yank, and put, in place of the more standard terminology cut, copy, and paste. Starting with the same letter, put and paste are easily interchangable. Once you get used to the word yank, it becomes synonymous with copy. But Vim’s use of the word ‘delete’ is problematic.

In most contexts, the word delete just means remove. But Vim’s delete operation copies the text into a register, then removes it from the document. In other words, Vim’s delete operation behaves like a cut.

That raises a question: if Vim’s delete command is really a cut operation, does Vim have anything equivalent to a true deletion? The answer is slightly oddball: writing to the ‘blackhole register’ performs a true deletion.

Standard terminology Vim's jargon
paste put
copy yank
cut delete
delete delete and write to "blackhole" register

Nothing returns from the blackhole register. It’s the Vim register equivalent of /dev/null. To perform a true deletion in Vim, prefix the delete command with "_ for example: "_diw would delete the current word without saving a copy in a register.

I find Vim’s terminology to be unhelpful, so I try to make a point of describing the d{motion} command as “cut the specified text” (rather than delete). Likewise for x, c, s, and so on.

Further reading