Articles
Back to TopMenu...
 

Vim Registers

One of the beauties of vim is its registers. As a developer, it will be difficult, if not impossible, to avoid vim registers in your workflow. In this article, we'll take a closer look at Vim registers.

What Are Vim Registers?

Vim registers are spaces in memory that Vim uses to store some text or operation details. Each of these spaces has an identifier so that it can be accessed later.

OS Clipboard, which we use to copy and paste, is conceptually identical to Vim registers, except that Vim registers have many spaces available to you, some of which are automatically filled by Vim in response to certain actions.

Clipboard and Vim Registers Analogy

This multi-memory structure and the internal workings of Vim registers make it difficult to lose copy when you are familiar with Vim registers. But the downside is a bit of a learning curve to overcome.

Vim Registers Basic Usage

Vim gives you the possibility to see the registers with their contents. You can list all registers and their contents by using the :registers (or :reg for short) command.

:reg
--- Registers ---
""   register " content
"0   register 0 content
"1   register 1 content
.
.
"9   register 9 content
"a   register a content
.
.
"z   register z content
"-   register - content
"*   register * content
"+   register + content
".   register . content
":   register : content
"%   register % content
"/   register / content
"=   register = content

The first column contains the name of the registers, and the second its contents.

You can also see specific registers and their contents by using the :registers command follow by the names of the registers :registers a b c (or :reg a b c for short)

:reg a b c
--- Registers ---
"a   register a content
"b   register b content
"c   register c content

Normal Mode

In Normal mode, each register is accessed following the pattern "<register-name><command>.

" : prefix of register,
<register-name>: register name
<command>: the command to perform on that register.

For example, command "ky yanks the current selected text and stores it in the register "k, and "kp pastes the current content of register "k to the buffer.

Insert Mode or Command Mode

In Insert or Command Mode, you can access the registers by pressing Ctrl + r and <register-name>.

For example, Ctrl+r p in Command mode will paste the content of the “p register after the curror in the command line.

Types of Vim Registers

If we individually count the vim registers, we will have 48 registers. They are grouped into 10 types of registers according to their functions and characteristics.

Vim Rgister Types

The Unnamed Register ""

Vim will update the unnamed register whenever you delete (using the d, c, s or x commands) or yank (using the y command) some text, regardless of whether or not a specific register was used (e.g. "xdd, you will see that the register "x will contain the deleted line as well as the register "").
The only exception is the "_ (black hole) register.

Vim uses the contents of the unnamed register for any put command (p or P) which does not specify a register. Additionally you can access it with the name ".

The 10 Numbered Registers "0 to "9

The numbered registers will be updated by Vim with text whenever you use yank, change or delete commands.

Numbered register "0 contains the text from the most recent yank command, unless the command specified another register. If in any doubt you want to paste the most recent yank, you can use “0p.

Numbered register "1 contains the text deleted by the most recent delete or change commands, unless the command specified another register or the text is less than one line (the small delete register is used then).

With each successive deletion or change, Vim shifts the previous contents of register "1 into register "2, "2 into "3, and so forth, losing the previous contents of register "9.

The Small Delete Register "-

The smal register contains text from commands that delete less than one line, except when the command specifies a register.

For example, if you delete the word precious from the text with the diw command. After this action, the unnamed register "" and the small delete register "- will contain precious.

The 26 Named Registers "a to "z or "A to "Z

Named registries will only be updated if you tell Vim to do so. The lowercase letter and the uppercase letter point to the same register, except that you specify them as lowercase letters to replace their previous content or as uppercase letters to append to their previous content.

To record macros, the unnamed register, numbered registers and named registers are used. But preference is given to named registers due to the volatility of the unnamed register and numbered registers. For more information on macros, see :h recording in Vim.

The Three Read-only Registers ":, "., "%

These three registers cannot be modified on the instruction of the user. Like their name, you can only read in these registers with the p, P and : put commands in normal mode and with CTRL-R in insert and command mode.

  • “. Contains the last inserted text
  • “% Contains the name of the current file.
  • “: Contains the most recent executed command-line.

The Alternate Buffer Register "#

This register contains the name of the alternate file for the current window. Switching between buffers, the command CTRL-^ switches to the alternate buffer (the buffer that you have just left). The alternate file register will change how the CTRL-^ command works.

This register can be edited with the command :let @# = value and value can be an existing buffer number or an existing buffer name.

The Expression Register "=

This register is different from other registers which store text. This register allows the evaluation of any expression including Vimscript functions. It's pretty cool when you need to do some simple calculations and insert the result into the document.

When typing the = after " in normal mode or CTRL-R in insert mode 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.

For example, considering you're in insert mode in this file at the end of the line. ... the total sum is $. Now hit CTRL-R = and type 10*6.2+7*3.5+4*1.52<CR> And the result will be ... the total sum is $92.58.

The Selection and Drop Registers "*, "+ and "~

Vim offers the "+ and "* registers to reference the system clipboard. Note that on some systems like Windows, "+ and "* are the same, while on others they are different. Generally on Linux, "+ and "* are different: "+ corresponds to the desktop clipboard (XA_SECONDARY) that is accessed using CTRL-C, CTRL-X, and CTRL-V, while "* corresponds to the X11 primary selection (XA_PRIMARY).

The read-only "~ register stores the dropped text from the last drag and drop operation. When something has been dropped onto Vim, the "~ register is filled in and the pseudo key is sent for notification.

The Black Hole Register "_

Like a real black hole, anything you place in the black hole register can’t escape (be read). When reading from this register, nothing is returned. The black hole registry is the only register when used, do not leave a copy in the unnamed register.

It makes no sense to use the black hole register with the yank command. If you want to delete or change text without affecting the other registers, use the black hole register.

For example, "_dd will delete the current line without the ability to paste the deleted line elsewhere.

Last Search Pattern Register "/

After each search, Vim places the search pattern in this registry. You will therefore find the most recent search pattern in this register. This register is used to iterate (n N) and highlight (hlsearch) search results.

You can't yank or delete into this register. For example, “/yy will not affect the search pattern register.

However, you can write to the search pattern register with the command :let. For example, :let @/ = “What are we looking for”.

Conclusion

For reference read Registers or use the command :help registers in Vim. With All of the above, you can attest that Vim registers are a great tool in Vim.
But with years of experience with Vim registers, you will start to get frustrated and feel that some features of Vim registers are half-implemented and terminology misleading.
You can optionally correct them with a combination of commands. Learn more by reading “Registers: The Good, The Bad and The Ugly” by Drew Neil.