They say that names are the beginning of abstraction. Giving something a name allows us to hold it in our minds, study it, understand it, use it, manipulate it, transform it. Without a name, we have nothing at all to go on. Consequently, as simple as it is, the ability to name and recall things is enormously useful. We’ve already seen that Vim allows you to name locations (marks), and commands (macros), and we’ve seen how useful these can be. In addition to these, and perhaps most commonly, you will also want to name content selected from the text you are working on.
Vim allows you to name arbitrary pieces of text by putting them in “registers.” You can prefix many common Vim commands with "
to name the result of the command (in Vim parlance to “put it in the register” with that name) or to recall the text with that name. For example:
"ayy
= yank line and name ita
"ap
= paste the text whose name isa
"ad}
= delete paragraph and remember it asa
"Ayap
= append this paragraph to the registera
- Note the capitalized name here. We name content using lowercase, and append to it if needed using uppercase. Vim interprets lowercase as defining and uppercase as appending to the same register.
As a general guideline, if you’re working with more than two pieces of text at the same time, it could be helpful to name at least one of them. For instance, let’s say you want to put these words in alphabetical order:
cherry durian banana apple elderberry
One way to do this is to repeatedly apply the “swap” phrase we learned recently. Another way is to name these words while deleting them, so that you can paste them back in the right order using their names.
Just as Vim maintains convenient standard marks based on your activity, so does it maintain convenient pieces of text that you may need in standard registers. One way to think about these context-aware registers is that they are pronouns — names that stand in for nouns. We’ve learned that Vim has a grammar, and that this grammar usually expects verbs to be used together with nouns. How then does Vim know what to do with p
? This is a verb, and yet, we’ve specified no noun in the context of which the verb must operate. Vim knows what to do because it maintains a register called "
(the “unnamed” register) which always contains the most recent text copied or deleted. We can think of this register as the pronoun “it.” When we say ddp
Vim essentially treats it as ""dd""p
, just as the sentence “Alisha found an old coin in the sand and took it,” could also be read as “Alisha found an old coin in the sand that we can call ‘it,’ and took it.” Other “pronoun” registers, i.e. registers that automatically refer to text in the context of your activity, include .
(last inserted), 0
(last yanked), 1 - 9
(recently deleted — note that the behavior of these varies across Vimlikes; for instance, in Vim these don’t include “small” deletions, while Evil and possibly Neovim may include all deletions. Read your documentation to understand how these will work), "/
(last search term). There are more.
Another common case where registers are handy is when there’s some text that’s special in some way, and needs to be referred to frequently. For instance, let’s say a new hangout spot opened up in town and you want to move all your appointments there. We want to change every line here to show “Vim and Emac’s” instead of the current venue.
Meet at Southside 10am at Reverie 4:30 at Tied House See you at Yancy's
Simply copying the new name and pasting over the old venues won’t work, because each time you do so the replaced text will overwrite the text you want to paste which is in the “it” register. Instead, name the new venue (call it a
, say) and then replace each venue with something like "ap
after visually selecting it.
While there are many types of names in Vim, note that these content “registers” are not the same as the location “marks,” so you don’t have to worry about conflating marks and yanked content. On the other hand, the content registers are the same ones used by macros. In other words, Vim sees macros simply as text, and they can be written and edited just like any other piece of text. This means that you could record a first attempt at a macro using q
, try it out, and if it doesn’t work as expected, paste the register into a buffer, edit it, yank it back into the register, try it again, debug it, and fix it so it works correctly. Just like writing code!1
More Fun with Names
We said earlier that naming is simple and yet powerful. An example of the power names give us can be seen in the ability to define persistent nouns. Earlier, we talked about how search functions in Vim give us “ephemeral” nouns. They’re ephemeral because as soon as we search for something new, the noun we just defined is gone. What if we wanted to keep it around? The good news is, this is simple to do — all we have to do is, you guessed it, give it a name! If the noun you happen to be interested in is occurrences of the pattern [^,]\+
, then simply yank this text into a register. Now, if you type /C-r<name>
(C-r
can be used in any insertion state (including insert mode) to paste the contents of a register), this restores this motion noun to the n
and N
keys (and corresponding text objects to the keys gn
and gN
). This may be useful in text that is formatted in a unique way where the usual motion commands aren’t as useful as some more tailored motions. For instance, in navigating a CSV, it could be handy to have the noun [^,]\+
, which matches CSV elements.
1 If your macro contains “non-printing” characters like Enter and Escape, refer to your documentation for how to handle this (e.g. in Vim proper, use C-v
to insert such characters “literally.” Evil and other Vimlike users, chime in in the comments if you know how to do this in your editor!).
Golo
This is really useful. Precise and well thought out writing!
Neovim uses C–v for inserting non-printing characters too.
sid
I’m glad to hear that, and thank you for confirming the Neovim binding!