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
:h quote_alpha
:h :yank
:h :global
:h :reg
- Practical Vim, Tip 60: Grok Vim’s registers
- Practical Vim, Tip 99: Collect TODO items in a register