DEV Community

Cover image for What are registers in Vim, how to use them and the best practices.
Zean Qin
Zean Qin

Posted on • Updated on • Originally published at zean.be

What are registers in Vim, how to use them and the best practices.

Overview

A register in Vim is basically a slot in memory that you can put a piece of text in and read it out later. Each register is like the clipboard provided by the OS (e.g. Windows, MacOS etc.), except that Vim has a LOT of them instead of just one.

It takes a while to learn how to use them and some slow repetitions in the beginning to develop the muscle memory, but you'll never go back to the simple yank/paste again after getting comfortable with it.

Basic usage

You can list all registers and their contents by using the :reg command, or using the :reg <register-a> <register-b> ... to filter to the specific registers and their contents.

Each register is accessed following the pattern "<register-name><command>. Note it's got 3 parts,

  • the " prefix,
  • the <register-name>, and
  • the <command> to perform on that register.

For example, "ay yanks the current selected text and stores it in the register "a, and "ap pastes the current content of register "a.

Registers

There're 9 different types of registers provided by Vim. I've listed them all below, but only included detailed explanation (with some common practices) for the ones I've used the most. For the advanced ones, I've included a link to the official documentation.

1. Unnamed (or default) register

The " register (accessed via "") is the default register.

Its content will be updated whenever you delete (using the d, c, s or x commands) or yank (using the y command) some text.

You can read the content of this register by using "". For example, you can do ""p to paste the value store in it.

Vim reads the content of the unnamed register for any put command (p or P) which does not specify a register. So, p is the same as ""p

2. Numbered registers

These are the registers "0, "1, "2, "3, "4, "5, "6, "7, "8, "9.

"0 will be set by the most recent yank command, therefore it always has the content of the latest yank.

If you yanked some text, you can always refer to it using "0p.

"1 will be set by the most recent delete or change command only if the deleted or changed text is longer than one line (also called big delete).

"2, "3, "4, "5, "6, "7, "8, "9 stores the other 8 latest big deletion or change. With each successive big deletion or change command, Vim shifts the previous contents of "1 into "2, "2 into "3, and so forth, losing the previous contents of "9.

3. Small delete register

The register - is set by a delete command only if the deleted text is smaller than one line (also called small delete).

Tip: The latest yanked texts will always appear in "0. The latest small deletion will appear in "- while the latest big deletion will appear in "0. The other latest 8 big deletion will appear in "2, "3, "4, "5, "6, "7, "8, "9.

4. Named registers

These are the registers "a to "z, or "A to "Z.

These registers are only used when you say so. Specify them as lowercase letters to replace their previous contents or as uppercase letters to append to their previous contents.

Tip: I only use a couple of these as more permanent storage for code snippets such as console.log(` `), as compared to the temporary storage provided by the numbered registers and the small delete register.

I find myself can survive with just the basic registers mentioned so far, and rarely use the ones below. But if you're interested, keep reading.

5. Read-only registers

These are %, #, : and ..

You can use them only with the p, P, and :put commands and with CTRL-R.

". contains the last inserted text (the same as what is inserted with the insert mode commands CTRL-A and CTRL-@).

Note: this doesn't work with CTRL-R on the command-line. It works a bit differently, like inserting the text instead of putting it (textwidth and other options affect what is inserted).

"% contains the name of the current file.

"# contains the name of the alternate file.

": contains the most recent executed command-line. Example: Use @: to repeat the previous command-line command.

The command-line is only stored in this register when at least one character of it was typed. Thus it remains unchanged if the command was completely from a mapping.

6. Expression register

The expression register is the "=. According to the documentation,

This is not really a register that stores text, but is a way to use an expression in commands which use a register. The expression register is read-only; you cannot put text into it. After the '=', the cursor moves to the command-line, where you can enter any expression. All normal command-line editing commands are available, including a special history for expressions. When you end the command-line by typing <CR\>, Vim computes the result of the expression. If you end it with <Esc\>, Vim abandons the expression. If you do not enter an expression, Vim uses the previous expression (like with the "/" command).
The expression must evaluate to a String. A Number is always automatically converted to a String. For the p and :put command, if the result is a Float it's converted into a String. If the result is a List each element is turned into a String and used as a line. A Dictionary or FuncRef results in an error message.
If the "= register is used for the p command, the String is split up at <NL\> characters. If the String ends in a <NL\>, it is regarded as a linewise register.

Please see the documentation for details

7. Selection and drop registers

These are the "*, "+, and "~.

They're used for storing and retrieving the selected text for the GUI. As I've never used this, I won't write down all the details for it. If you're interested, you could read the documentation for details.

8. Black hole register

The "_ is the black hole register. As the name implies, nothing will happen if you write to it, and nothing will be returned from it if you try to read its content.

Tip: You can direct your deleted text to the black hole register to avoid affecting other normal registers.

9. Last search pattern register

This is the "/ register, and it stores the most recent search pattern.

According to the documentation,

This is used for n and hlsearch. It is writable with :let, you can change it to have hlsearch highlight other matches without actually searching. You can't yank or delete into this register. The search direction is available in v:searchforward. Note that the valued is restored when returning from a function function-search-undo.

References

Top comments (0)