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

Vimcasts

Using Vim's named registers

#53

We’ve met Vim’s default register and the yank register. This time, we’re going to look at the named registers, which are handy if you want to cut or copy some text that you intend to paste multiple times.

Download

OGG 5.1 MB

Quicktime 8.8 MB

From Vim’s documentation (see :h quote_alpha):

Vim fills these registers only when you say so. Specify them as lowercase letters to replace their previous contents or as uppercase letters to append to their previous contents.

Vim has 26 named registers: one for each letter of the alphabet

The named registers are handy for copy and paste, especially when you want to paste the same text multiple times. Use "ay{motion} to copy text to register ‘a’, then "ap to paste text from that register into the document.

Note that when we yank text into a named register, Vim writes that text to the default register and to the specified register, but it does not touch the yank register.

Appending to a named register

Using an uppercase letter tells Vim to append text to the specified register (rather than overwriting the existing contents). For example, "Ayy copies the current line, and appends it to register ‘a’.

I prefer using the Ex command :yank A over the normal mode command "Ayy. In both cases, it would be easy to accidentally type a lowercase letter, which would cause the specified register to be overwritten. But in the case of the Ex command, it’s easier to see when you make a mistake, and easier to correct it.

Collecting all lines that match a pattern in a register

We can clear the contents of named register ‘a’ by running the command: qaq. The qa command instructs Vim to start recording keystrokes into register ‘a’, then the final q stops the macro recording. No keystrokes were recorded, so the register ends up blank.

Now we can use this Ex command:

:global/TODO/yank A

For each line that matches the pattern TODO, run the Ex command: :yank A.

Further reading