How I got to learn vim registers
One of the things that bugged me the most when I was learning Vim was copy and pasting.
For example, I would copy a line with:
I would go to the section I wanted to put the yanked line, and of
course, I would see a line that I had to delete first, so without
thinking, I would type:
dd. I would then quickly realize that
I had lost the line that I copied, go up to the copied line, yank the
line again, go down to the section again, and finally paste it.
Another case where copy and pasting bugged me was copying to/from the system clipboard. For example, I had no idea how to copy and paste between different Vim windows. I also had no idea how to copy from external pages into Vim other than highlighting the text from the external page, going into insert mode in Vim, and middle-clicking the mouse.
After feeling this limitation countless times, I finally decided to learn how the Vim registers worked, and how I could copy and paste at will.
Vim has something called registers, where various strings are stored, depending on the context and what actions you have taken.
For example, the ” register is Vim’s default register, so when you yank
or delete lines, they go into that register by default. The % register
holds the relative path of the current file. You can view the contents
of these registers by typing
You can perform actions with these registers by typing “, followed by the register name, then the action you would like to perform.
For example, if you would like to paste the current file’s relative path
twice, you would type
"%2p. You can achieve the effect of the
The null register
At this point, you’re probably wondering how this helps with the first issue I had with Vim, which is how I can avoid overwriting the content that I yanked in Vim. This is solved by using the _ register, which is Vim’s null register.
Let’s say you’ve copied a line with
and you have a line that you want to delete without losing the line
you’ve yanked. You can do that by typing
"_dd. Want to delete
lines 38 to 43 without overwriting the yanked line? No problem with
:38,43d_. Alternatively, you can select those lines in visual
mode, and type
Copy and pasting to and from the system register
To solve the second issue I had, I used two other special Vim registers,
namely the * and + registers. The * register is where content that you
highlight in linux operating systems go to, and the + register is where
content that you copy/cut with
Ctrl+X go to.
Now given the knowledge acquired from using the null register, you can
safely guess that
"+p would paste whatever you’ve copied using
Ctrl+C. Copying into the clipboard is as simple(?) as
which you can test by pasting into somewhere else with
you copy with
"*yy instead, you can paste it by middle-clicking
the mouse, just as you would do with any content you’ve highlighted in
Re-mapping the default yank, delete, and paste behaviours in Vim
I personally prefer using the Vim registers directly, so that I don’t forget how to use them, but there are definitely a lot of people who would prefer to have copy and pasting to behave like any other editors out there.
In that case, you could consider re-mapping the default Vim behaviours by adding something like the following into your ~/.vimrc:
d to delete into the null register
nnoremap d "_d vnoremap d "_d nnoremap D "_D vnoremap D "_D
c to not change the default register’s content
nnoremap c "_c vnoremap c "_c nnoremap C "_C vnoremap C "_C
y to copy into the system clipboard
nnoremap y "+y vnoremap y "+y nnoremaP Y "+Y vnoremaP Y "+Y
p to paste from the system clipboard
nnoremap p "+p vnoremap p "+p nnoremaP P "+P vnoremaP P "+P