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.
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,
<command>to perform on that register.
"ay yanks the current selected text and stores it in the register
"ap pastes the current content of register
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.
" register (accessed via
"") is the default register.
Its content will be updated whenever you delete (using the
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) which does not specify a register. So,
pis the same as
These are the registers
"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
"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).
"9 stores the other 8 latest big deletion or change. With each successive big deletion or change command, Vim shifts the previous contents of
"3, and so forth, losing the previous contents of
- 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
These are the registers
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.
You can use them only with the
:put commands and with
". contains the last inserted text (the same as what is inserted with the insert mode commands
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.
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
:putcommand, 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.
"=register is used for the
pcommand, 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
These are the
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.
"_ 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.
This is the
"/ register, and it stores the most recent search pattern.
According to the documentation,
This is used for
hlsearch. It is writable with
:let, you can change it to have
hlsearchhighlight 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