vim

What are Vim Registers

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).

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

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.

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.

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).

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.

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.

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.

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.

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.

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.

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.

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.

To replace the text, I will again use the “xyy.

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.

To print the file name, use the “%p command.

To print the last command, use the “:p.

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.

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.

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.

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.

To access the value of the expression register, use the :reg “= command.

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

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.

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 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.

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.

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.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.