Main Content

Recording (a macro) in vi

Archive - Originally posted on "The Horse's Mouth" - 2010-06-27 11:55:52 - Graham Ellis

There are time that I'm editing a file with vi, and I note that my editor says "recording on the bottom of the screen / status bar.

What have I done? Is this useful? Can / should I turn it off?

• If you type "q" followed by another keystroke in command mode, you start recording into a macro buffer who's name is the same as the second keystroke - so "qa" means start recording macro a.

• When you next type "q", the macro recording will cease.

• When you then type "@" followed by the macro letter, you'll rerun the key strokes that you recorder in the macro.

Example ... move over the middle of a line of text in vi then

qa (record)
dlph (swap the current character with the next one along)
q (end recording)

then

@a (to swap the charactets back)

and you can use @a anywhere else to swap characters in your edit session.

I've concluded that the reason I find myself recording sometimes when I hadn't intended to is because I key too quickly - q is very like :q, and the q key is right beside the tab key. But at least I now know what the worrysome message means, how to get out of the recording mode - and I've learned about a useful new procedure into the process!