Registers are always seem a scary thing, but they are not. A simplified version would be something like, you have a lot of clipboards to work with.
There are some special registers, and the rest are just storage. We briefly mentioned them before, but now let’s see how can we use them.
A lot of actions can work with registers, like d
, y
, or p
. I think this
is the list of the most used actions with registers. When we delete something or
yank it, it will end up in a register. When we paste in something, it will come
from a register.
We can list all out registers and their content with :registers
. I’ll refer
registers with "a
where it’s the a
register, or "0
for register 0
.
There are some special registers:
""
: Content of the last changed register. Imagine it as a pointer."0
: The text from the most recent yank command."1
: The text deleted by the most recent delete or change command."%
: Name of the current file. (read only)":
: Most recently executed command. (read only)"#
: Name of the alternate file for the current window. (read only)".
: Contains the last inserted text. (read only)"+
: Clipboard register. (system clipboard)"/
: Most recent search pattern."_
: Garbage can register.
There are a few others, but I’ll skip them as they are complicated and it’s not something we use daily, weekly, or even monthly.
We can set registers with :let @register=value
, for example copy the name of
the current file into the system clipboard we can use :let @+=@%
. It’s
basically “set the clipboard register value to be the name of the current file”.
As we mentioned earlier Macros are stored in registers too.
How to use registers?
Prefix the action with "[register]
for example "ayy
will copy the current
line into "a
. If we want to paste it we can do it with "ap
.
Garbage can register
There is one register that can be super useful and it’s the garbage can
register. I don’t know what is the proper name, I call it garbage can because
whatever I send there, it will be lost and does not update ""
. For example if
I want to delete a block of text, but I don’t want to change the state of my
registers I can visual select it and use "_d
to delete. That way all my
registers stays the same as before.
Capital letters
Named registers are all lowercase, and the reason is simple, if we use capital letters it means append and not replace. Very few articles mention this, but it’s super helpful. For example if we want to build up a text from multiple lines, but not all of them, most of the users start to move lines around, but in reality they can use append on a register.