1. Overview
In this brief tutorial, we’ll look at how we can paste text in insert, normal, and command mode. First, we’ll go through the most used registers available in Vim. Then, we’ll see the registers in action.
Moreover, these methods are also applicable to Vim variants such as NeoVim.
2. Vim Registers
A Vim register is a kind of bucket in memory that we can use to store our text. So, we can access the text later when we need it. It’s the same thing as a clipboard where we store data to be pasted later. Vim provides both in-built and custom registers. These registers have an identifier that we can use to yank (copy) and paste text from.
We can access the register through the “ key. For instance, if we want to yank text into the b register in normal mode, we can press “byy to yank the current line into the b register. Now, if we want to paste from the register, we can press “bp.
There are different registers in Vim, but we’ll be covering the essential registers that we use daily.
2.1. Default Register
The default register is probably the most used register in Vim. It stores the text that has been deleted or cut through the d, x, s, or c key. The default register is identified by the “ symbol.
2.2. System Clipboard Registers
The second most used registers are the system clipboard registers. Now, we have two types of registers when it comes to the system clipboard. The primary and the secondary clipboard are identified by the * and +, respectively. It’s up to the Linux distribution to prefer one of these two. However, typically, when we use the middle-mouse button, the text is yanked into the primary system clipboard register. On the other hand, copying the selection using <*Ctrl+v> or the copy action, the text is yanked into the secondary system clipboard register.*
3. Pasting Text in Vim
3.1. Pasting from Primary System Clipboard
To paste text from the primary system clipboard register, we can use the * symbol. Let’s copy some text into the primary clipboard using xclip:
$ uname -a | xclip -sel p
The command will copy the output of uname into the p or primary clipboard. Now, we can use the “*p combo to paste the text in Vim while in normal mode:
On the other hand, we can also paste text in command mode using the <Ctrl+r>* combo:
3.2. Pasting from Secondary System Clipboard
Pasting from the secondary system clipboard is the same as pasting from the primary one. As we know, we can access the secondary clipboard through the + symbol. We can use the “+p and <*Ctrl+r>+ in normal and command mode, respectively.*
However, it might be more convenient if we use the <Ctrl+Shift+v> hotkey since it does the same thing as “+p. Similarly, <Ctrl+Shift+c> will yank the text into the + register to be available for pasting outside of Vim.
3.3. Pasting from the Default Register
Text that has been deleted or cut goes into the default or unnamed register. For instance, if we have recently deleted a line through the dd combo, it’s deposited into the default register. Therefore, we can either undo the line or paste it elsewhere from the “ register in the current document.
To paste the text in normal mode, we can use the “”p combo. Mind the two double quotes – one for selecting register and the other for the default register. However, we shouldn’t rely on this register as it can be overwritten. Instead, we should use a separate register to store reusable text.
In the command mode, we can press <Ctrl+r>” to paste the text from the unnamed register.
4. Conclusion
In this tutorial, we briefly discussed the different essential registers in Vim and how we can access these registers to yank and paste text.
Moreover, for a very in-depth dive into the registers, type :help registers in Vim’s command-line.