Registers in vim are a great feature to store text snippets and even to run commands on the text stored within them. However, I'm a tidy person and tend to clean things up when I'm done.
I know that if I wanted to clear register a
, I can use qaq.
I can also execute the following command:
:let @a = ''
However, these solutions seem like a mere workaround to the problem. When I execute :registers
, the list still displays register a
(with an empty value), while registers that have otherwise never been used are not displayed.
Is there a way to clear a register with the side-effect of removing the register from this list?
And if so, is there also a way to clear all registers at once, i.e., to reset that list of registers?
To clear the a register, for instance, I type q a q to set the a register to an empty string. Equivalently, :let @a='' does the same. Then, looking at the output of :reg is still helpful because it is very easy to discern between empty registers and contained registers.
You can also access the registers in insert/command mode with Ctrl-r + register name, like in Ctrl-r r . It will just paste the text in your current buffer. You can use the :reg command to see all the registers and their content, or filter just the ones that you are interested with :reg a b c .
Vim has ten different types of registers: 26 Named registers “a to “z (or “A to“Z) The small delete register “- 10 numbered registers “0 to “9.
Since that venerable answer on the mailing list, linked by @romainl, we have setreg('a', [])
that clears the register.
Thus, the code could become:
let regs=split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-"', '\zs') for r in regs call setreg(r, []) endfor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With