Using Neovim and Vim Registers: A Detailed Guide

In this article, we discuss how we can copy and paste from multiple registers in vim/Neovim effectively. This article is meant for vim user who has knowledge of the basic commands in vim. This article explains the basic concepts of copy and paste commands, vim registers with examples, and also discusses how we can add keybindings for faster copy-pasting from/to registers.

What is cut, copy, and paste in VIM?

Vim does not offer cut and copy. Instead, Vim offers yank(y), delete(d). The yank command is synonymous with the copy command, and the delete command is functionally similar to the cut command. There is nothing interesting about how copy and paste works – the copy command saves the selected part of the text in a named memory location called register (not to be confused with registers in the CPU). You can now paste the text somewhere else in the document or maybe in another document.

What is special about vim copy and paste?

Vim is special, and so is yank and paste in vim. Unlike many other editors, vim offers its own set of registers. Most of the text editors rely only on the registers maintained by the operating system, for the yank and paste operations.

This can become overwhelming at a point in time, where you need to copy/cut from multiple sources at a time. In a normal text editor, you have to copy and paste a single selection and then move on to the next selection. How ineffective!!!

Vim, being the god of editors, allows you to be more efficient and error-proof copy-pasting without any hassle. But to get to that point, we need a bit more knowledge about the registers that vim has to offer.

Understanding the multiple registers in VIM

Although vim offers a multitude of registers (each has its own special purpose), we are interested in a few important ones, and we will understand the types of registers one by one.

  1. The Unnamed Register: Vim doesn’t normally interact with the OS register for yanking or deleting. Everything you yank or delete something without mentioning the register (eg: x/X, d/D, y/yy) are stored in the unnamed register.
  2. The Numbered Registers: Vim offers numbered registers from 0 to 9. These registers also have special purposes
    • Number 0 – Stores the content of the last yank
    • Number 1 – Stores the content of the last delete
    • Number 2-9 – Stores the content of the previous deletes in respective order
  3. The Named Registers: Vim offers named registers denoted by lower case alphabets (a-z) specifically for our own use. The 26 named registers have a special optional property – we can to append to (instead of replace) the text in the register when necessary.

Apart from these vim specific registers, you can use the system registers for recording text outside of vim. There are two system registers:

  • + : This is the default system clipboard. We use Ctrl+Shift+C for pasting the contents from the clipboard to the shell.
  • * : This register is used to store the currently selected item in the system. We generally use Ctrl + Shift + s for pasting the contents of the ‘select’ clipboard to the shell.
  • "- : This register is referred to as the Small Delete Register, and it primarily stores deletions or changes that are less than one line.
  • “_ :There is also one Black Hole register, which you can use when you do not want Vim to store any deleted text. This acts as the /dev/null of registers.
  • “# : The alternate file register comes in handy when you open multiples file in Vim at the same time.
  • “= : The Expression Register allows you to store and execute commands directly from the Vim.
  • There are also three read-only registers, which are “:, “., “%

Now that you know about the various register names, we can take a look at how can we utilize them while using Vim/Neovim.

How to copy and paste from multiple registers in VIM?

Here are some of the commands in Vim, with examples of how to use them with multiple registers.

1. Yank to multiple registers in VIM

:["x]y

The (y)ank command yanks (copies) the selected text into the register x (if specified) else it is yanked to the unnamed register.

Examples:

# Yank the selected text into the unnamed (default) register
:y

# Yank into register a
:"ay

# Yank into the system clipboard
:"+y

2. Paste from multiple registers in VIM

:["x]p

The (p)paste command pastes text from the register x if specified, else it pastes from the unnamed register.

# paste from the unnamed register
:p

# paste from register a
:"ay

# Paste from the system clipboard
:"+y

Pro Tip: You can append to an alphabet register by using Uppercase letters for yanking. Example:

# Yank and replace the current text in register a
:"ay

# Yank and append to the current text in register a
:"Ay

Note: The vim integrated registers work for the buffers opened under the same vim process. The registers are not updated concurrently for two different vim processes. For copy-pasting between two different vim processes, the system board is the best option.

Customizing VIM for increased productivity

Vim provides tons of features and to be honest no one uses all these features at once. Moreover, copy-pasting from/to register is a little complex. To deal with this problem, you can set the custom vim shortcuts for commands you use regularly. Here are some of the suggested key re-mappings that can help you out in faster copy-pasting

" Set a key-mapping for copy text and pasting to the system clipboard
:vnoremap <Leader>y "+y
:nnoremap <Leader>p "+p

" Set key-mapping for dealing with two alphabetical registers easily
" Two does the work for me, you can set more

:vnoremap <Leader>a "ay
:vnoremap <Leader>A "Ay
:nnoremap <Leader>a "ap

:vnoremap <Leader>x "xy
:vnoremap <Leader>X "Xy
:nnoremap <Leader>x "xp

You can add similar key-mappings to your .vimrc or init.vim for smooth and faster copy-paste experience.

Conclusion

I hope you learned something new about copying and pasting and using Vim registers from this article. There are always more new commands and combinations to learn in vim, but this article should serve as a good starting point for your day-to-day coding/text editing. If you require more help, you can always refer to the inbuilt manual by pressing :h [search-term]. If you liked this article, stay tuned for more such articles on vim.