Sunday, April 8, 2012

macronomicron

The only thing better than macros in Vim are recursive macros.*

(* Actual betterness may not apply.)

Cryptic Overview for the Gifted

1. first - use recursive macros
2. stop the ride - have an error condition
3. the quaker - qaqqa
4. secret sauce - j0@aq
5. blow the raspberry: @

Slightly Deeper Explanation for the Rest of Us

Macros are cool. They are referred to as complex repeats in the Vim docs. See  :help q   for the mechanics of recording and then read on for playing back macros. For the lazy, here is a brief recap:

q{0-9a-zA-Z"}   begins recording into the specified register. All movements, insertions, deletions, :ex commands, etc, are recorded until you press   q   again. So, the command   qagUwWq   creates a macro that upper-cases the current word (from the cursor forward) and then moves to the next WORD (skipping whitespace and punctuation). To run this macro on the next word, we use the command   @a   and after that, to run it on another word we only need to use   @@   (which repeats the last macro command, conveniently.)

If you've already got a partial command in a lettered register (a-z), you can append more command to it by using the   q   command with the upper-case form of the lettered register (A-Z)

There are quite a few other nifty tricks you can pull with macros, so I implore you to head on over to   :help :@   and guzzle that goodness up. But here, I'm going to continue talking about recursive macros - macros that call themselves. Recursive macros are one way to repeat a series of commands across multiple chunks of text all automatically so you don't have to hit   @a   or even   @@   over and over and over... your @ key thanks you now.

Stop the ride

If you don't provide an action within the body of your macro that causes an error, the macro will run from the point of execution until the end of the file. This may be what you want; it may not. To have it stop somewhere specific, you'll have to ensure it errors at that point. Using the    FfTt   commands are a common way to force a failed search. If you use a pattern search with the   /   command, you may need to use anchors to cause failures.

The quaker

Because a recursive macro executes itself with the command   @a   (assuming the macro is being recorded in register   a), then at the point in time that the macro is being created, the contents of register   a   *before* this recording began will be inserted into the macro body when it sees   @a. Unless you know what you're doing, you probably want this to be empty. To this end, we use   qaq   as a fast way to empty register   a. By recording nothing into   a, we effectively empty it. The subsequent   qa   in the command string   qaqqa   begins the real recording.

Secret Sauce

To allow the recursive invocation of your macro to be actually useful,  you have to arrange for it to start in the right place. This could be on the next line, or at the next word, or next paragraph, or block, or... etc. The command   j0    moves down a line and to the first column of the screen, ready for the subsequent recursive invocation with the command   @a   and switching off recording with   q   hence   j0@aq

Blow the Raspberry

You're done recording. Now you need to run the sucker. We do that with   @a   (presuming you recorded your macro into register   a)

Next: Get a better understanding of recursive macros in Vim.