Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Using Vim's named registers

#53

Run time:

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.

Shownotes

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

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..