Vim magic - Registers
About the 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-paste (in the good sense of the word) :)
It is difficult to imagine working in an 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 a few other special registers where Vim places, for example, the text you searched with /or text that you drag-n-drop into the editor window. To indicate that you want to copy or delete text into a specific register, you must specify its name before the command. For example, “ayy will copy the entire string to register ” a . But not all registers can be written, some of them are read-only (and there is even one only for writing).
A little more special, Vim's magic of register-related magic for seeding, so as not to be lazy to read to the end:
- 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 “clipboards” into which you can add text for a long time 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 supplementing it.
Vim has the following types of registers:
1. An unnamed register "" . A kind of "default register". The text is placed here if you did not specify case 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. When using this register, the contents of " "does not change.
2. Numbered registers "0 - " 9 .
Register “0 contains the last text that was copied ( y ) without a register. That is, a regular yank will put the text in the registers "" and "0 , and " my will change the contents of the registers "" and "m , but will leave the contents " 0
unchanged . Register "1 contains the last deleted text, provided that no deletion was specified case and text contains at least one full line (otherwise, instead of "1 , a special register is used " -) Register "1 still in use, when the removal was done using motion commands % , ( , ) , ` , / , ? , n , N , { and } . With each subsequent deletion of the text, the old contents of the register "1 is placed in the register " 2 , the contents "2 is in this case in " 3 and so on, 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 case was not explicitly specified when deleting.
4. Named registers «a - » z or «A - » Z . If you often need to paste the same text and you do not want the copy or delete operations to overwrite it, use the named registers “a - ” z .
Also, named registers can be used to “accumulate” deleted or copied text. To do this, use registers «A - » the Z .
5. Read-only registers ": , ". , "% and"#
": contains the last contents of the command line.
". contains the last inserted text (this text can also be inserted in paste 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)
6. Register of expressions " = . A special magic case is used not to store text, but to insert the results of expression calculations. For example, in insert mode, you can typeCtrl + R = 5 + 5 * 5 , press Enter and 30 will be inserted into the text . Expressions can be much more complex, use the values of registers, variables or options of Vima. More in: 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, for In order to copy text from Vim to another application, you copy (or cut) the text into the "* case " and then paste it where necessary. To copy text from an external application into Vim, you copy the text in the external application and paste it into Vim from the register "* . The difference between the registers " * and "+is only in X11. They are described in detail in : help x11-selection .
Read-only register "~ contains the text that was dragged into the Vim window.
8. The black hole register " _ . This is a write-only register. It is used in order not to change the contents of the default register "" and digital registers "1 - " 9 during deletion . When pasting from this register, nothing happens.
9. Register of the last search "/ . It stores the expression that was used during the last search. The contents are also used by the commands n and N, and also for highlighting text when the 'hlsearch' option is on .
To view the contents of the registers, use the command : reg . : reg without parameters will show the contents of all registers. To see 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 the contents of the register m into the system clipboard, in which you “accumulated” a bunch of useful text ( “My )”, you can write : let @ * = @ m .
Happy vimming!
It is difficult to imagine working in an 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 a few other special registers where Vim places, for example, the text you searched with /or text that you drag-n-drop into the editor window. To indicate that you want to copy or delete text into a specific register, you must specify its name before the command. For example, “ayy will copy the entire string to register ” a . But not all registers can be written, some of them are read-only (and there is even one only for writing).
A little more special, Vim's magic of register-related magic for seeding, so as not to be lazy to read to the end:
- 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 “clipboards” into which you can add text for a long time 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 supplementing it.
Vim has the following types of registers:
1. An unnamed register "" . A kind of "default register". The text is placed here if you did not specify case 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. When using this register, the contents of " "does not change.
2. Numbered registers "0 - " 9 .
Register “0 contains the last text that was copied ( y ) without a register. That is, a regular yank will put the text in the registers "" and "0 , and " my will change the contents of the registers "" and "m , but will leave the contents " 0
unchanged . Register "1 contains the last deleted text, provided that no deletion was specified case and text contains at least one full line (otherwise, instead of "1 , a special register is used " -) Register "1 still in use, when the removal was done using motion commands % , ( , ) , ` , / , ? , n , N , { and } . With each subsequent deletion of the text, the old contents of the register "1 is placed in the register " 2 , the contents "2 is in this case in " 3 and so on, 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 case was not explicitly specified when deleting.
4. Named registers «a - » z or «A - » Z . If you often need to paste the same text and you do not want the copy or delete operations to overwrite it, use the named registers “a - ” z .
Also, named registers can be used to “accumulate” deleted or copied text. To do this, use registers «A - » the Z .
5. Read-only registers ": , ". , "% and"#
": contains the last contents of the command line.
". contains the last inserted text (this text can also be inserted in paste 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)
6. Register of expressions " = . A special magic case is used not to store text, but to insert the results of expression calculations. For example, in insert mode, you can type
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, for In order to copy text from Vim to another application, you copy (or cut) the text into the "* case " and then paste it where necessary. To copy text from an external application into Vim, you copy the text in the external application and paste it into Vim from the register "* . The difference between the registers " * and "+is only in X11. They are described in detail in : help x11-selection .
Read-only register "~ contains the text that was dragged into the Vim window.
8. The black hole register " _ . This is a write-only register. It is used in order not to change the contents of the default register "" and digital registers "1 - " 9 during deletion . When pasting from this register, nothing happens.
9. Register of the last search "/ . It stores the expression that was used during the last search. The contents are also used by the commands n and N, and also for highlighting text when the 'hlsearch' option is on .
To view the contents of the registers, use the command : reg . : reg without parameters will show the contents of all registers. To see 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 the contents of the register m into the system clipboard, in which you “accumulated” a bunch of useful text ( “My )”, you can write : let @ * = @ m .
Happy vimming!