In vim, we can paste (p
/P
) after deleting (d{something}
) or yanking (y{something}
). Pasting uses Vim registers. However, do you know that there are 10 different registers in Vim? Check out `:h registers.
I am using neovim v0.3.4. Some of these might be different if you use regular vim, but the gist should be the same and most people should be able to follow along.
In this article I will go over 3 of them because I believe these are the more useful ones (plus it's easier to learn 3 things than 10 things in one sitting). In the future, I will cover more of these registers.
According to Vim, these are the 10 registers. The three we will cover here are shown in bold.
Some operators saves the values to registers:
s, x, y{something}, c{something}, d{something}
I use the mnemonic "sexy cd" - excuse my language 😳 - to remember them.
We use them all the time. When we yank/change/delete anything we are storing them to the unnamed register (""
).
We can use unnamed register by p
or ""p
.
If we do another yank/change/delete, it will replace the previous unnamed register. It is like your computer's standard copy/paste.
There are 2 different numbered registers: register 0 (yank register) and registers 1-9 (numbered register)
When you yank something (say you just yanked an entire line yy
), it gets saved in two places:
p
after you yy
)"0p
)Yank register only gets replaced when you yank something else second time. It won't get replaced if you run s/x/c/d.
Yank register also won't fill out numbered registers 1-9.
This is useful when we yank an important line, that yanked line is always available even after we perform several deletions so you can call it anytime.
Numbered registers store the latest 9 deletions/ changes (s/x/c/d) automatically.
Let's try it. Copy/paste the following: w
With the cursor on the last line ("5. five"), let's do 5 dd
.
Cool. 1-5 should be gone and saved to registers 1-5. If we want to paste three, we can just do "3p
(read: get value from register 3 and paste it)
Note that this register stores deleted/ changed texts at least one line or larger. So deleting a line (dd
) gets stored in numbered register but deleting a word (diw
or de
) won't. Anything smaller gets stored in small delete (-
) register
Named register allows us to save our deleted/ changed/ yanked texts into register a through z. You can do it by "{letter} {delete/yank/change action}
.
For example, if I want to delete the entire line and save that into register a
, I can do "add
(read: save to register a all deleted line).
To get that value back, we can do "ap
(read: get value from register a and paste it).
If we want to append to existing register, we do "{uppercase letter} {delete/yank/change action}
. The key is to use the uppercase letter of the letter we want to append. So to add into our register a, we can do "Add
and use "ap
to get the value back.
We have just learned 3 Vim registers. I find that I used these 3 more than anything else. Start using them into your daily workflow today. I personally find yank registers and named registers very useful.
That's all folks. Vim till you win!