Tuesday, December 09, 2008

Registers

There is always something new to discover out there, particularly with Vim. I thought I knew how registers worked: you got your window manager registers (+ and *); the 'last thing I deleted' register ("); the confusing 'last 10 deletes' registers (0-9); some read only registers that I don't consider to be registers because, hey, you can't write to them (% and #); and then the multipurpose named registers (a-z).

I've programmed myself over the years to use specific letters for specific purposes. The 'n' and 'm' registers are reserved for recorded macros (yes, macros are saved in registers - handy to know if you want to save a macro in your .vimrc for later use, say), the 'iou' I tend to use for registers that are used in a macro, and 'fvc' for yanking and storing.

For instance, if the lines below existed, and I wanted to copy the first one for later use:

every good boy
does good

I'd do: "fyy

Now, if you type ":registers" you can see all of your registers, and thats overwhelming. I tend to just look at the register I'm especially intersted in ":echo @f" or ":registers f" does the trick nicely. Also, since I know I use the 'fvc' registers for yanking - I tend to just look in those registers if I forget what I've done with ":reg fvc".

Did you know that you can append to the named registers? I had NO IDEA. By using the capital letter 'F' instead of the lowercase letter 'f', for instance, you append rather than replace. Take the example above. Say that I was going along editing and I saw another line I might want to use later:

...
...
blown up beneath my glass.
Colors dazzle insect wings.
...
...

I could, if I wanted, just add it to register 'f' with the other line by doing: "Fyy

Type ":reg f" and you see:

--- Registers ---
"f Every good boy^JColors dazzle insect wings.^J


Not too pretty, but thats where this comes in handy: ":echo @f"

Every good boy
Colors dazzle insect wings

Press ENTER or type command to continue

There you are - a case for using 'echo' over 'register' and an interesting note about the named registers.