Finger Macros in Vim

One of the most productive tricks in vim is the finger macro.

Say you wish to assign a macro to the key `s` — we would take the following steps (note in the following the back-tick marks are to to identify the keys that one presses, and are not entered as part of the vim command):

1/ press `q` (this tells vim to start recording)

2/ press `s` (this assigns the macro to key `s`)

3/ enter the key strokes you wish to record

4/ press `q` again (this tells vim to stop recording)

We now have a macro recorded to `s`; to trigger that macro type `@s`.

How is this useful?

Say we have the following paths and we wish to change them (in this case, say you’re moving from Windows to Mac).

“C:/home/projects/R/file1.r”
“C:/home/projects/R/file2.r”
“C:/home/projects/R/file3.r”
“C:/home/projects/R/file4.r”
“C:/home/projects/R/file5.r”
“C:/home/projects/R/file6.r”
“C:/home/projects/R/file7.r”
“C:/home/projects/R/file8.r”
“C:/home/projects/R/file9.r”
“C:/home/projects/R/file10.r”
“C:/home/projects/R/file11.r”

Go to the head of line one and press the following keys ( means the escape key):

`qs0fC2xi~<esc>`

This will change the start from “C:” to “~”. You ought to see the edits on the line as they occur. Now if press `@s` it will play this macro and change the start from “C:” to “~” on another line. Go to any place on line two and give it a try.

Now if you have a column of these, you probably want to add a go-to-the-next-line instruction at the end of this command. To do so, you don’t have to re-record the whole macro — you can edit it just like text in any buffer.

To edit a vim finger macro go to a new line and do the following: `”sp` (that’s double-quote, s, p)

This command tells vim to paste the contents of buffer s into the line. There you will be able to review and edit the macro you previously recorded.

Following our example above, you should see a string like this: `0fC2xi~^[“

(the `^[“ is how vim represents <esc>. You can reproduce this with `Ctrl-v + [` … which means press Control+v together, and then release them and press `[`)

To add a new line to the end, simply go to the end of the line, and add `+`.

It should nowlook like this: `0fC2xi~^[+`

To put that into the macro buffer, press escape (to get back into command mode) go to the start of the line (use `0`) and then press the following in order: `”sy$`.

[If you wanted it assigned to some other key, say `t`, simply swap that key with the `s`: `”ty$`]

This will replace the old macro-command-string with the new one. Now if you go to the head of line 2 and press `@s` it will replace the “C:” with “~” and then go the the head of the following line.

Now this property allows us to use the repeat command with the macro, to edit multiple lines.

Assuming you are now on line three, now press: `9@s`.

This will change the head all of the remaining lines to “~”.

As an extension, say we also wanted to add something to the path.

For example, say we wanted to add a folder ‘old’ after R, so that we have ‘../R/old/’. To do this use `0fC2xi~^[fRa/old^[+`.

Remember, however, that the `^[` must come from a `Ctrl-v + [` — vim can tell the difference.

Advertisements
This entry was posted in coding, vim and tagged . Bookmark the permalink.

please comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s