Jump to content

What are Vim Registers


Linux Hint

Recommended Posts

Vim registers are storage blocks used to store yanked, deleted text and operations. These registers can be accessed using their tag names, which can be a number, letter, or symbol.

If you are using Vim, then you must be using the Vim registers. Every time you copy/delete text, or perform a command operation, Vim stores this information in the registers. When you paste it, Vim gets the data from the register and draws it into the buffer. In other words, Vim registers act as a clipboard to store the text and command history.

Vim has a total of 10 distinct types of registers, each of which serves a distinct purpose. In this tutorial, I will be exploring Vim registers, their types, and usage.

Note: For this tutorial, I am performing commands on macOS. Vim is a cross-platform application, therefore the commands mentioned in this guide will apply to all Vim versions irrespective of the operating system.

Vim Register Operators

The registers in Vim can be employed using operations such as copying, deleting, or changing. Each operation has specific operators, such as to yank text y operator is used while to delete text, the d operator is used. A list of commonly employed operators is given below:

yy Yanks (copies) the line
dd Deletes the line
cc Deletes the line and enables the INSERT mode
s Deletes the character and starts INSERT mode to substitute
x Deletes the character under the cursor
q<reg> Macro; stores the set of commands to a specific register

To print the stored text, p or P operators are used:

p Put (paste) the text after the cursor
P Put the text before the cursor

These commands also take the count. For example, to copy 10 lines use the 10yy command, similarly to paste the 10 copies of a yanked line use 10p.

Listing Registers

To list all the registers use the :register or :reg command, and to list the specific register use the :reg β€œ<reg>, where <reg> is the register name (e.g., 0-9, a-z).

Vim-Registers-1.png

In the same manner, to list the content of multiple registers, use the :reg β€œ<reg_1> β€œ<reg_2> β€œ<reg_3> command.

Vim-Registers-2.png

In the register list, you will see three columns, Name, Type, and Content. Here, the Type means the content type, not the register type. Three types of content can be modified with registers.

  • l: Line-wise
  • c: Character-wise
  • b: Block-wise

For example, if the content is copied using line-wise operation (yy) then the content type will be l, if it is character-wise (yiw) then the type will be c, and if the content is copied block-wise (ctrl+v and y) then the type will be shown as b.

Vim-Registers-3.png

Working with Vim Registers

Vim registers can be used to store the content using the yank, delete, and change operators. The stored content can be accessed using the put operators.

Saving Text to a Register
When you press the yy key to copy a line, it will be saved into the default register called the unnamed register (β€œβ€). To store the copied line to a specific register, the quote (β€œ) followed by the register name and operator are used.

"<reg><operator>

For example, to copy a line to a register name z use β€œzyy. Similarly, if you have selected multiple lines in the VISUAL mode, use β€œzy to store the lines in the z register.

Accessing the Register
To access the Vim register, the quote (β€œ) symbol is used with the register name and p/P operator.

"<reg>p

For example, if the register name is z and to access the content of thIS register, use β€œzp in the NORMAL mode. However, to perform the same operation in the INSERT mode, use ctrl+r z.

β€œ<reg><operator> NORMAL Mode To store the content use quote (β€œ), register name (a-z), and operator (y, yy, d, c, or cc)
β€œ<reg>p NORMAL Mode To paste the content of a register use quote (β€œ), register name (a-z) and p/P operator
ctrl+r <reg> INSERT Mode To paste the content of a register, use ctrl+r and register name (a-z)

You have the basic idea of the Vim registers. Now, I will go for an in-depth discussion of each register type and its usage.

Types of Vim Registers

In Vim, there are 10 types of registers, designed for specific purposes.

  1. Unnamed Register (β€œβ€)
  2. Numbered Registers (0-9)
  3. Small Delete Register (β€œ-)
  4. Named Registers (a-z or A-Z)
  5. Read Only Registers (β€œ:, β€œ., β€œ%)
  6. Alternate File Register (β€œ#)
  7. Expression Register (β€œ=)
  8. Selection and Drop Registers (β€œ*, β€œ+, +~)
  9. Black Hole Register (β€œ_)
  10. Last Search Pattern Register (β€œ/)

1. Unnamed Register (β€œβ€)

It is the default register of Vim to store yanked or deleted text using y, yy, d, dd, s, or x commands.

The :reg β€œβ€ command can be used to list the content stored in the unnamed register.

Vim-Registers-4.png

To paste the unnamed register content, use the put command (p or P) command. By default, the p command puts the content stores in the unnamed register. Alternatively, the data stored in the unnamed register can also be pasted using the β€œβ€p command.

In the following example, I have copied a line using yy command and then put it 5 times using count 5 with p (5p).

Vim-Registers-5.gif

2. Numbered Registers (0-9)

The numbered registers are used to store the last yanked or deleted text.

The register 0 is different from registers 1-9. The register 0 will always store the last yanked text. For example, if I yank a line using yy command, the line will be stored in both the unnamed and the 0 registers.

Vim-Registers-6.png

Now, if I delete a line using dd, the deleted line will be stored in the unnamed register and register 1 while the last yanked text will remain in register 0. So, the last yanked text can be accessed anytime through register 0.

Vim-Registers-7.png

The registers 1-9 store the last deleted text. For example, if I delete a line using the dd command, the deleted text will be stored in both the unnamed and register 1. Register 1 will store the latest deleted text. If I delete another line, the previously deleted text will move to register 2, and the latest will be stored in the unnamed and register 1. In essential, the numbered registers (1-9) store the history of deleted text.

Let’s understand the numbered register with an example. The following Vim text file comprises 10 lines; let’s yank the line 3 using yy command. Now, let’s check the status of the registers using the :reg command.

Vim-Registers-8.jpg

The yanked line is currently in the unnamed and 0 registers. Now, let’s delete all the lines one by one using the dd command. So, last line 10 will be in the unnamed register and register 1. At the same time, other deleted lines will be stored in registers 2-9 in ascending order.

Vim-Registers-9.jpg

These lines can be accessed using the p command. For example, to put the text of register 9 use β€œ9p. Furthermore, if you want to put the value of register 9 5 times, use β€œ95p.

3. Small Delete Register (β€œ-)

This register stores the deleted text if it is less than a line, using commands like x, daw, diw, dab, and dib. Moreover, if you delete some text using VISUAL selection mode, and if it is less than a line, then it will be stored in the small delete register. In Vim, a line means a complete sentence ending with a period.

For example, in the following file, I selected 3 words collaboration and support from line number 9 and deleted them using the d operator. The small delete register will be used to save this selection since it is not more than a line long. Use the :reg β€œ- command to list the content of the small delete register.

Vim-Registers-10.jpg

4. Named Registers (a-z or A-Z)

The named registers are used to store user-specific text. So, if you want to store text in a custom register, then 26 registers from a to z can be used.

For example, I am yanking a line in the following file using β€œzyy, the text will be stored in the z register as shown in the following image.

Vim-Registers-11.png

The lowercase and uppercase named registers have different purposes. For example, if the text is saved in the lowercase register, so in order to replace the text of that register the lowercase register will be used. However, to append the text to the lowercase register, the uppercase register with the same name will be used.

Let’s comprehend it through an example. From the following file, I stored line 8 in register x using β€œxyy.

Vim-Registers-12.png

To append text after this line, I will first select a few words in the VISUAL mode. To append the selected text, I will use the β€œXy command.

Vim-Registers-13.gif

To replace the text, I will again use the β€œxyy.

Vim-Registers-14.gif

5. Read-Only Registers (., %, πŸ™‚

Vim has 3 read-only registers that store the inserted text, file name, and last executed command.

. Stores the last inserted text
% Stores the file name
: Stores the last executed command

These registers as the name suggested cannot be modified, however, can easily be accessed.

To put the last inserted text, use the β€œ.p command.

Vim-Registers-15.gif

To print the file name, use the β€œ%p command.

Vim-Registers-16.gif

To print the last command, use the β€œ:p.

Vim-Registers-17.gif

The @: command can be used to repeat the previous command in Vim. In the following example, I have deleted the first 3 lines, on pressing @:, 3 more lines are deleted, hence repeating the previous command.

Vim-Registers-18.gif

6. Alternate Buffer Register (β€œ#)

The alternate buffer is a file that is opened in the current buffer. The β€œ# register stores the file name of the buffered file in the current buffer. Let’s open another file in the same buffer using the :e ~/.vimrc command, note that the current file name is file.txt.

Vim-Registers-19.gif

To close the opened buffer, use the :bdelete or :bwipeout command. Now, it is possible to see the alternative file name saved in the β€œ# register.

Vim-Registers-20.png

7. Expression Register (β€œ=)

The expression register is a unique register that is not used to store text, instead, it stores the expression. For example, if you have a few numbers in the file, to sum these numbers, Vim expressions will be used.

This register can be accessed using β€œ= in the NORMAL mode, and ctrl+r = in the INSERT mode.

For example, to find the difference between two numbers, press β€œ=, and the equals sign will appear in the status bar; now type both numbers with the minus sign (-) in them and press the Enter key. Now, to print the difference, press p or use the :put command.

Vim-Registers-21.gif

To access the value of the expression register, use the :reg β€œ= command.

Vim-Registers-22.png

Similarly, to get the absolute value of two numbers, use the =abs(value1-value2) expression.

Vim-Registers-23.gif

In the same way, to store the output of the system command, this (expression) register can be used. For example, if I want to read the content of the file I will use =system(β€˜cat <filename>’), on pressing the Return key the content of the output will be stored. Using the p or :put command, the content of the output can be placed in the buffer.

Vim-Registers-24.gif

There is a range of expressions that can be used in Vim. To learn more about Vim expressions, use :help expression and :help expression-syntax commands.

8. Selection and Drop Registers (β€œ*, β€œ+, β€œ~)

Vim has two selection registers β€œ* (quotestar) and β€œ+ (quoteplus) which are used to store the text from the GUI. These registers are used to copy text from external programs such as browsers or any other word processor.

For example, if you copy a text selection from a browser, using ctrl+c or cmd+c it will be stored in the β€œ* register. However, you cannot use p or :put to paste this text in the Vim editor. Use β€œ*p to put the text into the Vim editor that is copied from external applications.

It is important to note that both β€œ+ and β€œ* registers are connected to the clipboard, so both (β€œ*p, β€œ+p) can be used to paste the text into the Vim editor. Similarly, to put text from Vim, use β€œ*yy to yank a line and then paste it into any GUI application, use ctrl+v or cmd+v.

It is essential to note that if both registers (* & +) are doing the same thing, then what’s the point? Vim has two selection registers β€œ*, β€œ+ doing the same job and that is because of the X11 window system. The X11 provides two methods to store the text, one is selections and the other is cut-buffers. The selections are owned by the application and are lost upon closing the application, while cut buffers keep the text stored in the X-server. For these two types of storage, the β€œ* and β€œ+ registers are used. To learn more about it, use :help x11-selection command.

Next, the drop register β€œ~ which is used to store the text from the last drag and drop operation. For example, if you drop a text selection from an external application, it will be stored in the drop register.

If you find pasting from the external application using three different keys difficult, then you can map the selection to be stored in the unnamed register.
Open the vimrc file, place set clipboard=unnamed in it, and save the file. Now, whenever you copy from an external application, it can easily be pasted using the :put command or p key.

Note: The drop register (~) will only be available if the Vim is compiled with +dnd. Currently, it is only available for GTK GUI.

9. Black Hole Register (β€œ_)

If you perform a copy or delete operation in the Vim editor, by default it stores the text in the registers. If you want to delete the text without storing it in a register, use the black hole register. This register will not modify the unnamed register (β€œβ€) on deleting text.

For example, if I want to delete all the lines without storing them in the unnamed register, then I will first select all the lines using ggVG and then press β€œ_d keys to delete all the lines. Similarly, to delete a single line to a black hole, use β€œ_dd.

10. Last Search Pattern Register (β€œ/)

As the name suggests, this register saves the last search pattern using the / or ? operators. For example, if you search /Linux, then using β€œ/p will paste the Linux word.

Vim-Registers-25.png

Vim uses this register for navigation with the n and N keys after the search.

It is a writable register, its content can be changed using the :let. For example, to save Linux to this register, use :let @/=’Linux’.

Registers and Macros

In Vim, the macros are also saved in the named registers (a-z). If you are not aware of macros, then macros are set commands stored in a register. This set of commands can be executed by simply using the @ sign followed by the macro register name. Essentially, macros eliminate the need to type multiple commands repeatedly.

For example, to store a macro to a register m, I will use qm<set-of-commands>q. I want to move to the 5 lines down using 5j and delete the line using dd. Use, qm to start the macro recording, and then press 5j and dd. Now, to quit the macro recording, press q. To verify whether the macro is stored or not use, :reg β€œm or :reg m.

Vim-Registers-26.png

To execute these commands with a macro register, use the :@m command.

Clearing a Register

There is no straightforward approach to clearing a register because most registers don’t need to be cleared because they will eventually be overwritten. However, there are a few techniques to clear a register value in the Vim editor.

1. Record an empty macro to clear a register. For example, to clear register m, use qmq.

Vim-Registers-27.jpg

2. Set empty text using the let. For example, to clear the register m, use :let @m=”.

3. Use the setreg() function with an empty string as an argument. For example, to clear register m use :call setreg(β€˜m’, ”).

Conclusion

There are 10 different registers with different functionalities, which makes it almost impossible to remember. If you are a beginner then I would recommend keeping in mind only three registers, unnamed register (β€œβ€), numbered registers (0-9), and named registers (a-z).

To store text to a register, use a quote (β€œ) sign with the register name followed by an operator command (y, d, c). To paste the text from a register, use p or P commands preceded by quote (β€œ) and register name.

You must have heard of it, if you are doing a task more than twice, then automate it. Vim registers can be extremely useful to automate tasks and ultimately enhance the experience. To learn more about Vim registers, use the :help registers command.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...