Info about editor with macros

gumnos (Tim Chase) gumnos at
Thu May 20 13:55:42 UTC 2004

I use vim for the most part, and I don't think vanilla vi had the the
ability to record macros.  However, it did have the ability to do
abbreviations.  For recorded macros, they're recorded in vim with "q"
followed by the letter of the register in which you want to put them.
You then terminate recording by pressing "q" again in command mode.  So,
to record to the "a" register, you'd do "qa" followed by the actions you
want to record, followed by "q" in command mode.  Then to playback that
"a" register, you'd use "@a".  You can repeat the playback multiple
times by prefixing it with a count, like "22 at a" will playback the macro
recorded in the "a" register 22 times.  Using "@@" to "playback the most
recently executed macro" is a lot faster once you've done it once--so
you can use "@a" to try it out once, and then as long as you don't run
any other macro register, you can subsequently use "@@" to run the same
macro again.

For more information in vim, check out

    :help complex-repeat

which should put your help at the top of the section on recording and

Generally, these don't last over multiple sessions (well, depending on
your viminfo settings, you might keep them for a while).

Other options are to use mappings.  Vim offers much more flexibility in
terms of these mappings--you can specify that they only occur in a
particular mode (insert, command, visual, etc) and whether they are
recursive or not.

You can put these mappings in your .vimrc file if they're things you do
frequently, though it's a bit more complex if you need to do multi-line
stuff.  Something like this totally off-the wall example should get you
going in the right direction:

    :map <f2> G?\<index\>\c?<cr>

(each character is typed literally--those really are less-than and
greater-than signs)  This maps the F2 key to go to the bottom of your
document, and search backwards for the first instance of "index" (case
insensitive "\c", whole word "\<...\>"). (footnote*)

You can then just hit F2 and pop to the last instance of the word Index
in your document.  There's a lot more you can do with this, but the
nuances of entering non-standard characters (like function keys, line
breaks, or control characters) may differ across flavors of vi.  This is
the vim example, but in most distros of Linux, vim is the default vi.

Without knowing what sorts of macros you want, it's hard to give you
further suggestions.  If they're just one-shot recordings for a
particular document, then the macro recording works well.  If they're
something you intend to use regularly, putting a mapping in your .vimrc
is likely a better way to go.

Lastly, much of what can be put in a macro can be automated via a ":g"
global command which will perform any action on lines that match a given
regex.  If you join the vim discussion list, and give a detailed
description of your problem, the list is usually pretty good about
getting a response back in 15-30 minutes, often getting multiple
solutions back within that short time.

Hope this helps,


(footnote*)  Yes, I know, for the pedantic in the crowd, this example
doesn't find it if "index" is on the very last line of the file.  For
that, set "wrapscan" to true, and change the "G" to a "1" so that it
starts at the first line of the file, and searches backwards, wrapping.

More information about the Blinux-list mailing list