About registers in Vim have already been written in previous posts, but I decided to put the info together and add a little. After reading this article, you will become real masters of copy-pasting (in the good sense of the word) :)
It’s hard to imagine working in the editor without using copy / cut / paste operations. Vim provides very powerful tools for this. So registers are where Vim puts those pieces of text that you copy with
y or delete with
c or
d , but not only. There are some more special registers where Vim places, for example, the text you searched for using
/ or the text you drag-n-drop into the editor window. To indicate that you want to copy or delete text in a particular register, you must specify its name before the command. For example,
“ayy will copy the entire string to a register
” a . But not all registers can be written, some of them are read-only (and even there is one only for writing).
A little more special, Vim's register-related magic for starters, so that it’s not too lazy to finish reading:
- Vim keeps a small history of deletions, so you can paste previously deleted text, even if after that you deleted or copied other text.
- Vim provides you with 26 named "clipboard" in which you can permanently add text and it will not be lost during copy or delete operations.
- Vim can copy or delete text without overwriting the contents of the register, but complementing it.
')
Vim has the following types of registers:
1. Untitled register
"" . A kind of "default register". Text is placed here if you did not specify a register when copying or deleting. However, even if you explicitly copy the text into the register
“a with
” ayy , the text will still appear in
“” . The only exception is the special register
"_ , which is described below. With the use of this register, the content
" does not change.
2. Numbered registers
"0 -
" 9 .
Register
“0 contains the last text that was copied (
y ) without specifying a register. That is, the usual yank will put the text in the registers
"" and
"0 , and
" my will change the contents of the registers
"" and
"m , but leave the contents
" 0 .
The register
"1 contains the last deleted text, provided that the register was not specified when deleting and the text contains at least one complete line (otherwise, instead of
" 1 the special register is used
"- ). The register
" 1 is still used if the deletion was done with using the move commands
% ,
( ,
) ,
` ,
/ ,
? ,
n ,
N ,
{ and
} . With each subsequent deletion of the text, the old contents of the register
” 1 are placed in the register
“2 , the contents
” 2 thus appear in
“3 and so further, the contents of the register
"9 is lost, being replaced by the contents of the register
" 8 before deletion.
3. Register
”- contains deleted text if it was less than one line and the register was not explicitly specified when deleting.
4. Named registers
"a -
" z or
"A -
" Z. If you need to frequently paste the same text and you do not want copying or deleting operations to erase it, use the named registers
"a -
" z .
Also, named registers can be used to “accumulate” deleted or copied text. To do this, use the registers
"A -
" Z.5. Registers are read only
": ,
". ,
"% and
"": contains the last contents of the command line.
". contains the last inserted text (this text can also be pasted in insert mode using Ctrl + A)
"% contains the name of the current file
"# contains the name of the alternative file (the file that was edited before the current one)
6. Expression register
"= . A special magic register is not used to store text, but to insert the results of expression calculations. For example, in insert mode, you can type
Ctrl + R = 5 + 5 * 5 , press Enter and paste into the text
30. Expressions can be much more complex, using the values of registers, variables or options of Vim. For more information see
: help expression .
7. Registers for selections and drag-n-drop
"* ,
" + and
"~ .
Registers
"* and
" + are used to communicate with the outside world. Under Win, for example, the contents of these registers coincide with the contents of the Windows clipboard. That is, in order to copy text from Vim to another application, you copy (or cut) the text into the register
"* and then paste it where you need it. To copy text from an external application to Vim, you copy text in an external application and paste it in Vim from the register
"* . The difference between the registers
"* and
" + is only in X11. They are described in detail in
: help x11-selection .
Register is read only
"~ contains text that has been dragged to the Vim window.
8. Black hole register "
_ . This is a write-only register. It is used in order not to delete the contents of the default register
" and digital registers
"1 -
" 9 upon deletion. Nothing happens when pasting from this register.
9. Last Search Register
"/ . It stores the expression that was used during the last search. The content is also used with the
n and
N commands, as well as to highlight text when the
'hlsearch' option is on .
In order to view the contents of the registers, use the command
: reg .
: reg without parameters will show the contents of all registers. In order to view only the contents of the registers
"3 and
" f write
: reg 3f .
And finally. You can change the contents of the registers with the command
: let . In order to write, for example, in the register
"/ write
: let @ / =" the " . Or, for example, to put into the system clipboard the contents of the register
m into which you previously" accumulated "a bunch of useful text (
" My ) , you can write
: let @ * = @ m .
Happy Vimming!