Vim tricks with multiple cut/paste buffers
by BenV on Mar.27, 2010, under Software
Ever find yourself in vi(m) with a piece of code that needs some editing, but to preserve your paste buffer you keep limping around first deleting the first part and pasting it through the file, and later doing it again but now for the second block of code?
Let’s use an example. Imagine this very simple piece of html:
Register example on some html text
Some text
Some more text
Now imagine we want to make it a bit more fancy, and want to add a hyperlink to a dictionary or something for all occurrences of the word ‘text’. Since we’re lazy, we only want to type the code for the link once.
Step by step:
- Add the link code on the first occurrence of the link, you’ll have to type it once anyway 😉
- We are going to use register ‘a’ for the first part of the link, the a href=”blabla” part. The end we put in buffer ‘e’. To yank the first bit into buffer a, yank as you always would, but start off by typing “a. So the total command could look like “ay/< to yank right from where your cursor is to the start of the next tag.
- Next, to make things fancy, we do the same with the end tag, except we yank to register b by using something like “b/< to yank.
- To make sure you yanked the correct stuff, type :reg to see the contents of your registers.
- Now paste the appropiate register at the right spots by using “ap and “bp. Isn’t that great? 😉
So summarized:
Show your what’s in vims buffers / registers:
:reg
Delete a line into buffer ‘a’:
"add
Paste buffer q:
"qp
Some more advanced tricks:
Add something to a buffer by using the capitalized register name. :
"Add
Editing register a manually:
:let @a = "this text is now the new buffer content. Hihi."
Search and destroy using a regexp to replace stuff with the contents of your register:
:%s/je moeder/\=@a/g
Have fun 🙂