Discovering the Power of Vim Registers

Martin Džejky Jakubik
2 min readMay 23, 2018

Today I was writing standard Typescript, as I do these days at work. I wanted to swap a couple of lines, as I sometimes do at work.

I placed my cursor on the line which needed to be moved and typed dd to cut it. Then I placed the cursor on the line which needed to be replaced. I wanted to delete this line and place the copied one in its place. So, again, I typed dd to delete it. Finally, It was time to paste the first line but I realized that I have screwed up — the contents of the clipboard have been replaced with the second line… Arrrgh, not again!

This happens to me a lot. In case you haven’t figured it out, I use Vim and the command dd both deletes a line and copies it to the clipboard. Sometimes, thought, I want to delete a line without copying it. I started wondering whether it is possible to delete a line without overriding the clipboard. Or perhaps having separate clipboards for copying and deleting…

Vim Registers to the Rescue

I quickly discovered that there are these things called registers in Vim. They act like separate clipboards and there are loads of them (even custom ones). You can specify which register to use when copying or deleting text. It is also possible to use the black hole register to simply forget the text.

Now the fix to my problem. I can use the black hole register to forget the deleted line instead of copying it. To do that, simply type "_dd (notice the leading ").

Generaly speaking, to have a command use a specific register, you can just prefix the command with " and a letter indicating the register. You can read the documentation on which character represents what register. I mostly use _ for the black hole register and q for a custom register (you can use any letter from a to z for a custom register).

Hey! Thanks for reading, I really appreciate it. If you like this short article, make sure to give it a clap. If you want more, don’t forget to follow me. You know the drill. I mostly write about my personal experiences with technology — mainly Angular and related stuff.

Have a nice day!

--

--

Martin Džejky Jakubik

Frontend developer @ Exponea. Writing about things I learn along the way.