Background
I love using command-line tools. I have no idea why I find it so much fun but there you have it. As a result, I like to use terminal-based editors. I settled on Vim a very long time ago. If you want to know why, ask me and I’ll write a short post about my reasoning. I switched to using NeoVim a while back and have never looked back, however, I’m going to be using the term vim to refer to both of them interchangeably.
This is because if you’re handed an out-of-the-box, vanilla instance of them both, very few people will be able to tell the difference between them. Which is good because while I’m virtually guaranteed that any flavor of *nix I might use will have vim installed, the same can’t be said of nvim.
One of the things I like about using vim is that despite using it for decades, I’m always learning new things about it, and I’m somehow always surprised that I never figured it out sooner.
Registers
If you’ve used vim for any amount of time, you’ll probably know that it has registers. A register is a place where vim stores text for later use, e.g., a copy-and-paste buffer. Vim has a lot of registers with a lot of different purposes.
There’s the unnamed register, the numbered registers, the named registers, and the read-only registers. Then there’s the expression register, the small delete register (yep, that’s a thing), and the selection and drop registers. But at the end of the day, they all store text.
I am not going to go into exhaustive detail about all of them. If you want me to do that, please ask, I’ll be happy to write about it. Today I just want to cover the ones that seem to get the most use.
Good Old Copy and Paste
Before we get going, I want to present the syntax of accessing a register. This is done by typing ” followed by the register name or symbol while in normal mode. You paste the contents of the register using the P or p command. The uppercase command pastes the text in front of the cursor location, the lowercase command pastes the text behind the cursor location. To copy text into a register using the yank command you type, imaginatively, y.
The Y command will yank text from the cursor position to the end of the line in Vim versions 8.2 and beyond. Versions older than 8.2 yank the entire line. In all NeoVim versions, the behavior of the Y command is to yank from the cursor to the end of the line.
The Unnamed Register
As an example, the most used register that I can think of is the unnamed register. The unnamed register is the register where the text from the last delete or yank command gets stored.
Although it’s called “unnamed”, you can access it by typing the double-quote character. So to insert text from the unnamed register you type, `””p`. The reason it’s called the “unnamed” register is that if you don’t explicitly provide a register, that’s the one that gets used. You can accomplish the same task simply by typing `p` without providing the register name. The same thing goes for the yank commands.
The Named Registers
Moving to the next most used registers, we come to the 26 named registers. These registers are, “named”, as the letters a-z, hence 26 of them. You might think that you could double that by using A-Z as well, but that isn’t what the creators went with. Instead when you yank text into a named register using the lowercase letter, you overwrite any existing text. When you use the uppercase letter, you append the yanked text to any existing register contents.
Now, I have to confess, I’m lazy, and while the idea of the named registers sounded great on paper when I first discovered them I went about using them in the most idiotic way possible. By trying to remember where I stored all of the different text snippets I had yanked. This was obviously a poor plan, and as a result, I would rarely use named registers.
In fact, the only time I can recall using named registers in the past was when I needed to swap two words or pieces of text which required that I use a named register because I would overwrite the first word if I used the unnamed register. This later developed into an even worse habit of only yanking and pasting one word at a time because it was just easier than using registers.
I made a fundamental mistake. I allowed my lack of familiarity with my tool dictate how I used it. This is invariably a bad idea.
The Numbered Registers
Eventually, I simply forgot about the named registers. I mean, I knew they were there, and I even knew how to use the commands, but I never developed the habit, so I just forgot about them. It was like I’d blocked them out even to the point of not using plugins for code snippet management because I just knew they used registers and registers were a pain to use.
Then came the moment that I saw someone use the registers command, or reg if you’re lazy like me. In this case, they were using it to show the numbered registers. The numbered registers are numbered 0-9 and are accessed using “0, “1, etc., where register 0 contains the text from the last yank command, and registers 1-9 contain a rolling list of the last 9 change or delete actions with 1 being the most recent and 9 being the oldest. The contents of 9 are replaced with the contents of 8 when a new change or delete action is taken and the contents of 9 are rolled off the end of the list and lost. 1
Now, while they were looking for the numbered register contents, the reg command will show the contents of all registers that have text in them, including the named registers.
How had I been using vim for decades and not known about this? I felt like suddenly everything I’d ever been missing about using registers made sense and I was filled with a sense of endless possibilities.
Always Learning
And I won’t lie, I also felt like a right moron. How had I never seen, heard of, or researched this obvious answer?? This also highlights a rule I try to live by and frequently fall short of; re-evaluate what you think you know on a regular basis. You never know which of your assumptions or habits may be hindering you.
Now we come to the moment I reveal the true depths of my awe-inspiring stupidity in all its majestic glory. I use a convenience plugin called which-key. It provides a pop-up showing the possible keybindings that can complete the command you started to type. Guess what that includes? Correct! If I type the “ command, that popup appears with the register list showing you exactly what text is in which register. I had been so averse to using registers that I’d never once typed in the command to access a register during the entire time I’d been using the plugin. That was at least a year.
After the angelic hosts stopped singing and I began playing with registers and the reg command, I discovered by accident that the which-key had already taken care of the inconvenience of managing registers this entire time if only I’d bothered to look. I was so averse to registers that even reading the homepage of the plugin when I first looked at it I didn’t even recognize that it solved my long-standing complaint against registers.
There is a caveat to the numbered list. While the rolling list behavior of the numbered list described above is correct, it only applies to change and delete actions taken on text that is at least a single full line of text. The reasoning being that larger text changes and deletions are more valuable for later retrieval. There is no way of changing this behavior, however, you can use scripting or key remapping to automate populating the named registers.


