Editing Registers in Vim: RegEdit.vim
I recently came up with the most satisfying way to edit registers in Vim I’ve ever seen. I hope you like it as much as I do.
In Vim there are these things called registers, which you can think of as “a
bunch of copy buffers.” If you, for instance, highlight some text, and run the
"ay, you will “yank” the text into register
Registers are also used for macros, one of the simple ways that a user can
quickly automate a task. Imagine you wanted to surround ten lines of text with
quotes and append a comma. You could could start a macro in register
and then add the quotes and comma around the current line:
I"<Esc>A",<Esc> and then
move down to the next line:
j and then end the macro:
q. So all together
The Traditional Edit Method
Sometimes you’ll make a mistake. One way to handle this is to give up your
partially complete macro and start over. Another way is to complete the macro
and leave in the mistake, so if you moved left right before adding the
the example before, you’d include a couple backspaces and move right, or
Often leaving in such mistakes doesn’t matter, but sometimes these mistakes are
ok for some of the text and in other lines they break the macro entirely. What
I have typically heard you should do, in this case, is paste the register in
your document (
"wp), modify the contents, and the copy it back out (
I don’t like this. I am always worried I’ll accidentally copy an extra newline or drop one. I always feel weird treating my current document as a scratch space. I don’t even want to start a fresh empty file for something like this since it seems like such overkill. So I came up with something that I think is better.
Instead of setting the register using normal mode commands, you can set them as
variables in ex mode. The basic syntax is
:let @a = "some text". Of course
that only helps in setting the registers, not editing them.
I built a mapping (and a couple somewhat generic functions) that you can use to
populate the command-line with
:let @a = "current contents of @a". The usage
<leader>Ea to edit the
As a bonus it replaces special characters (like
named variants. So instead of seeing
^[, you’d see
Do you hate Command-line mode?
The next tip, on top of this, is that you may not love editing text in command
mode, because you have to use the arrow keys or whatever to move around. If you
<C-f> you will be “upgraded” to the
cmdline-history window. This is
what you have probably stumbled upon by accident by pressing
q:. It lets you
edit the command using normal mode, and you can then press enter to execute the
command. (By the way, if you accidentally hit
q:, the best way to get out
is to just press
<Enter>, since it starts on a blank line.)
And that’s it! It was a lot of fun building this plugin, even though I’m sure I spent a lot more time writing and documenting it than I would have saved using it. Special thanks to Wes Malone and Rik Signes for their help and suggestions.
If you’d like to learn more about vim, I can recommend a few excellent books. I first learned how to use vi from Learning the vi and Vim Editors. The new edition has a lot more information and spends more time on Vim specific features. It was helpful for me at the time, and the fundamental model of vi is still well supported in Vim and this book explores that well.
Second, if you really want to up your editing game, check out Practical Vim. It’s a very approachable book that unpacks some of the lesser used features in ways that will be clearly and immediately useful. I periodically review this book because it’s such a treasure trove of clear hints and tips.
Third and finally, if you want to really grok the guts of advanced vim, to write a plugin for example, you should really check out Learn Vimscript the Hard Way by Steve Losh. I expect to reread it two or three more times. I got the PDF version so I could read it while offline.Posted Fri, Oct 20, 2017