Monday, March 11, 2013

VIM Registers and Macros

TO KNOW: Basic VIM beyond :w of course.
TO KNOW: how to copy/paste/delete text.
TO WANT: Make your life much easier.


Registers in VIM may be a lousy term.  A better term might be buffers.  Vim stores a variety of buffers and uses them for a variety of tasks. Copy and paste AND Macros are just two examples.

Now, if you've never seen a vim register, type this:

1) Press [ESC] ...as many times as needed to ensure you are in command mode.
2) Type ':' {minus the quotes} and reg ( :reg) or (:registers).

If you did everything correctly you should see a messy list on the screen.  Pay CLOSE attention to the letters and/or numbers on the LEFT side of the screen.

"0  (blah blah)
"1 (blah blah)
"2 (blah blah)
...
"A (blah blah)

Each of these "0, "1, etc. represents a register -- a letter representing a buffer.  So what right? Well hold on OK!

In case you are wondering...

0 (yank register), 
1 to 9 (shifting delete registers), 
_ (like /dev/null, this is a black hole), 
" (default register, hence the Ctrl-R, "), 
- (small delete register), 
/ (the search pattern register), 
: (last command register), 
a to z for your own use (capitalized A to Z are for appending to corresponding registers).


Well they are important. Really, I get it, but these really are USEFUL!

COPY A BUFFER
First, let's get the contents into our document.

The command to get into the buffers are the double quote (") vs the command mode colon. (:)  Ya, it's strange but really...we're talking VIM here.

The sequence is...

(double quote) (name of buffer) (p for paste)

You may recall that p is pasting.

So, for example we want to access the k register -- assuming we see it in reg.

"ky  (" for the register access, k for k register, and p for paste)

If you did everything right, you will see the contents of the register in your document.

SO WHAT!!!!!!!!!!!!!!
Here is why.  This is ALL leading to Macros and it took me some time to gather these tidbits.  They are all out there...in pieces of documentation.

MACROS!!!!!!!!!!
Anyway:

Let's say you have a set of keystrokes you type OVER and OVER and OVER again.  BORING!
Macro it!!!

The command for macros (assuming command mode of course (press [ESC]) is

q (name of register or  buffer but everyone calls them registers)

So to record a macro in the b register type:

qb (NO COLON!!!!)
and you should see recording

EVERYTHING and I mean everything you type is now stuffed into the register.   Now as you type commands it will be recorded but STOP...I have to stop recording endless commands!

No problem

type
q to end the macro.  YA!

RUNNING A MACRO
:@ (letter of register)  (now you can use that colon)

So in our case...

:@b

But...it go away when me leave VIM so BAD RIGHT?

Yes and we need better English for me to understand your question so let's try again

But...it goes away when we leave VIM?

That's much better.

SAVING A MACRO FOREVER
Here's how.  Using our example of qb for the 'b' buffer and assuming we have the macro.  We need to get it into a document.

type

"bp

That command (not British Petroleum by the way) pastes the b register into the document. (Remember we did that earlier)

Now, load up .vimrc.  That's a hidden file that stores commands that vim can access.  Essentially, it's a huge configuration file.  If you don't have one -- shame on you -- make one and store it in your home directory on the linux workstation.

NOW...

Copy all those messy commands into your .vimrc...and it will work right?

ALMOST

Do this:

let @b = '  (your text in between the quotes -- single quotes mind you)'

Remember the text between quotes is the text from the register

(for this you would type "bp" to paste it in RIGHT BETWEEN the quotes.  This way you make NO typing mistakes

(save the file .vimrc but you know that right :)

NOW when you run vim...ya...it's remembered.  Isn't that AWESOME.

OH YA

to access the macro remember to type:

@b  (where b is the name of the register and @ is the command to run the macro.



No comments:

Post a Comment