Vim: Registers

This is part 10 of a series of tutorial to Vim. You can read Part 9 here.

Two Buckets

I bet you always wanted this. Even if you didn't know it existed, you secretly desired something like this.

Imagine you are editing some text, moving around some lines. You copied some text into your clipboard, came to the target location and realized before you can paste and empty your buffer, you want to remove some lines. And not just remove, you actually want to shift them somewhere else. Now you need the clipboard space for moving but it is already filled with previous text.

Consider another scenario, you have two distinct lines which you want to paste at different location based on the surrounding lines, that is, sometimes you want to paste line 1 and sometimes line 2.

Consider yet another scenario, you copied some lines and you realized, you want to access the text which was there in clipboard before you yanked. Wouldn't it be nice if there were 2 clipboards, 2 buckets, where in you could place two things at once.

Even better: Stack

Vim gives you even better facility: it gives you clipboard as a stack, along with several other general and special purpose buckets. It remembers last 10 items you yanked or deleted. These buckets are called as registers1. There are several categories of them, here we'll see some of them.

Lesson 11

Vim has multiple registers which are used for yanking/deleting/putting stuff into and from.

Registers

Whenever you yank or delete characters you need to specify a register which you want to put into; similarly while pasting (or putting), you need to specify which register to get characters from. Registers are specified by prefixing ". And the way you specify a register to yank/delete/put from is by prefixing register before the command. Thus, to yank into register "a, you'd say "ay<motion command>.

Named registers "a to "z

These are general purpose registers which you can use to store text2. When you use d, x, c or y along with register name, the content of the register is overwritten3. Storing text in named register is a good idea when you have same text to be paste multiple times along with some editing.

The Stack: registers "0 to "9

But, if you didn't specify any register, like we did till now, Vim will use the stack formed by registers "0 to "9. There is a slight difference in stack for delete and change commands and yank command:

  • Stack for yank command starts with register "0: If you do not specify a register, "0 will be used for storing yanked content. Previous content of "0 will shift to "1, "1 to "2 and so on, forming a stack.
  • Stacks for delete and change command starts with register "1. The stack grows "1 to "9.
  • It is worth noting that although the stack starts with different register for yank and delete commands, they share the same stack. So content of delete command will be shifted by a yank command. But last content of yank command will not be shifted by a delete command.
    • This can be helpful when you want to paste the last yanked text since it will not be disturbed by delete or change command.

If by reading this, you get a feeling that it is convoluted, I suggest you to see the stack in action for yourself. Yank and delete some text and observe the state of the stack (and all the registers used) by executing :registers or :reg for short.

Default Register

Unnamed register ""4 acts like a pointer to recently used register. Thus if you didn't specify a register while yanking, "" will point to "0 and "1 in case of delete command.

Black hole Register: "_

This is analogous to Unix's /dev/null, whatever is written to it is lost. This can be helpful when you want to delete/change something without disturbing the stack. For example: "_di{ will delete everything inside {...} without storing it in register.

Other Registers

There are several more registers which we haven't looked here. Some of the more interesting ones which you might want to look up would be:

  1. "*, "+ and "~ which help in pasting content copied from other GUI programs like web browsers and pasting in Vim using put (p) command.
  2. "%: contains name of current file
  3. ":: contains recently executed command-line.

Editing Macro

If you remember, in the previous part, we discussed how we can store sequences of keys in a register as a keyboard macro. If you lookup registers used (by executing :reg), you'll see all the macros recorded as text. Not only you can view them as text, you can edit them too. You can put them (p), edit the sequence of keys and yank the macro back into the same or different register.

Let's look at a macro edit in action. Suppose you recorded a macro to append 'w' end of line:

  1. qw: start recording macro in register w
  2. A: append to end of line (goto end of line and switch to insert mode)
  3. w: insert 'w' into buffer
  4. <esc>: go back to normal mode
  5. j: go down one line
  6. q: stop recording

You can see how it works by playing it couple of times using @w. Now, say instead of appending 'w' you want to append 'd' or even 'Donald Duck'. To do that we can edit existing macro rather than recording a new one:

  1. Paste the macro using "wp (paste the content of register "w)
  2. Edit the key sequences: replace 'w' with 'Donald Duck'
  3. Yank it back to register "w: "wyy assuming the content was in its own line.
    • We could have yanked it into another register and then use either of macros to append 'w' or 'Donald Duck'.

Summary

Command Comment
"reg[ydxc] You can specify a register by prefixing " and specify register before a yank, delete or change command
Registers "a to "z Named registers
Registers "0 to "9 Stack: yank starts with "0 and delete with "1
Register "_ Black hole register
:reg or :registers Lists all the registers in use
A Goto end of line and switches to insert mode. Similar to a. Both usually used to append text.

Click here for part 11

Footnotes:

1

:help registers to read official documentation

2

Macros are sequence of keys, which can be stored and manipulated as text

3

You can use upper case characters to append to the registers.

4

Here the register is " and the way to access register is by prefixing a ", hence we access the default register by ""

Creative Commons License

Date: 2017-06-26

Author: Anurag Peshne

Emacs 25.2.2 (Org mode 9.1.14)

Validate