sponsor Vim development Vim logo Vim Book Ad

RangeMacro : Execute macro repeatedly until the end of a range is reached.

 script karma  Rating 0/0, Downloaded by 875  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
Macros are a fantastic way to apply the same modification many many times.
Just record once and then repeat, repeat, repeat...

Though it's easy to repeat (@@, [count]@{0-9a-z".=*}) a macro, explicit
repeating becomes cumbersome when you're dealing with hundreds of repeats. You
can write a recursive macro (i.e. invoking the macro at the end), but that
will execute over the entire buffer, until the macro errors out on the border
of the buffer. Simple macros that mostly restrict themselves to a single line
can be repeated via :[range]normal @{0-9a-z".=*}, but that breaks when lines
are inserted or removed, or the macro jumps to other lines.

What's needed for extensive macro repetitions is to repeatedly execute the
macro over a range, and stop once the macro navigates out of the range.

This plugin provides a :RangeMacro command and mappings that execute a
recorded macro over a range, area of text covered by {motion}, or the current

- CommandWithMutableRange.vim (vimscript #3270) executes commands which may
  add or remove lines for each line in the range.

:[range]RangeMacro {0-9a-z".*+}
                        Position the cursor on the first column of the start
                        of [range] and execute the contents of register
                        {0-9a-z".*+} repeatedly until the cursor moves beyond
                        the lines covered by [range].

                        Repeatedly execute the contents of register
                        {0-9a-z".*+} until the cursor moves outside the text
                        covered by {motion}.

                        Position the cursor on the first column of the
                        selection and execute the contents of register
                        {0-9a-z".*+} repeatedly until the cursor moves outside
                        the selection. All selection modes are supported:
                        characterwise, linewise and blockwise.

Note: The check whether the macro moved outside the range is only done _after_
each macro invocation. During macro evaluation, jumps outside the range can be
used and will not stop macro execution.

To make a macro repeatable, the macro must move to the position where the next
macro call needs to take place, typically either as the first or last command
of the macro. The macro will always be executed from top to bottom of the
range, regardless of how {motion} or the selection was made.
So, for example, if the macro processes line(s) sequentially, append the "j"
command to move to the next line; if the buffer areas are located via
searching, you could use the "n" command to move to the next match.
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vba.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim RangeMacro.vba.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.

For a permanent configuration, put the following commands into your vimrc:

As there must be mappings for all supported registers, the mapping cannot be
easily customized via the <Plug> mechanism. Instead, if you prefer a different
mapping, redefine the start of the normal and visual mode mappings (before
the plugin is sourced):
    let g:RangeMacro_Mapping = '<Leader>@'

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
RangeMacro.vba.gz 1.00 2010-10-07 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.