Showing posts with label macro. Show all posts
Showing posts with label macro. Show all posts

Friday, June 12, 2009

Recursive Macros

A friend at work pointed out to me yesterday that he had accidentally written a recursive macro in Vim. I had never thought about the fact that this was possible, but I'd imagine there could be some potential usefulness for these somewhere. As a trivial (and useless) example, here's a recursive macro to move the cursor to the end of the current line one step at a time: qal@aq

Edit: A little research has shown that this is an excellent way to repeat a macro to the end of the file. Say that you have a file containing one number per line from top to bottom:

123
234
345
456

Put the cursor on the first line and type the following:

qaq (clears register "a" of any previous macros)
qa<ctrl-a><enter>@aq

Now type @a once more, and the macro will run to the bottom of the file incrementing the value on each line by one leaving you with the following:

124
235
346
457

Wednesday, April 30, 2008

Repeat Last Macro

There are times I want to repeat a macro just a few times. I know you can do count@{0-9a-z":*} but sometimes I don't feel like counting. You can use @@ to repeat the last macro, and it's an easy command to type very quickly, so it fits the bill.

Friday, February 8, 2008

Golf

Now for some fun ;-). A friend asked me for a Vim macro this morning to strip all but the text contained with the double quotes of a file with the following format:

<option value="Automotive">Automotive</option>
<option value="Banking">Banking</option>
<option value="Biotech">Biotech</option>

etc...

After the macro it would be:

Automotive
Banking
Biotech
etc...

Initially, I came up with the following sequence (macro storage command omitted):

df"f"d$j0 (9 chars)

This works fine, but my OCD kicked in, and I decided to make it shorter:

df"f"D+ (7 chars)

For a brief moment, I thought this was as short as I could make it, but then...

df"wD+ (6 chars)

As far as I know, this is the shortest possible macro to accomplish this task, but I would love to be proven wrong.

UPDATE:

My friend Chris Sutter has contributed another solution using text objects that is pretty nifty.

di"Vp (5 chars)

or if you want to jump to next line as with the previous 6 char macro:

di"Vgp (6 chars)

Good stuff... thanks Chris!

Friday, November 16, 2007

Macro Registers

Most Vim users know about macros and find them useful; however, until recently, I didn't realize they are actually stored in the same registers we all know and love. For example, you create a new macro by pressing `qa'. You type in a series of commands that you would like to repeat, end the macro, done. But wait, you made a mistake. Well, you can easily edit the macro stored in `qa'. I typically issue a :new to open a new window, move my cursor to that window and then "ap. The contents of the macro are printed to the window. From there, you can edit the macro as needed and then yank the macro back in to the desired register by executing something like 0"ay$. Once yanked, the new macro will execute as you would expect it would.