sponsor Vim development Vim logo Vim Book Ad

CommandWithMutableRange : Execute commands which may add or remove lines for each line in the range.

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

created by
Ingo Karkat
script type
This plugin offers enhanced versions of the :[range]normal[!] and :[range]call
commands which allow for additions and deletions of lines during iteration,
and adapt the initially supplied [range] accordingly.

The built-in :normal[!] and :call commands can take a [range], and are then
executed for each line in the range. If the supplied command / function
modifies the number of lines in the buffer, the iteration continues over the
initially supplied range of line numbers, totally oblivious to the changes.
Thus, if you want to apply modifications that add or delete lines before or
inside the [range], this built-in Vim functionality isn't much of a help. (You
can work around this by recording a macro and then manually repeating it over
each line, until you reach the end of the range, but you need to do the

- RangeMacro.vim (vimscript #3271) executes macros repeatedly until the end of
  a range is reached, also taking addition / removal of lines into account.

The plugin provides enhanced versions of the :call, :normal and :execute
Vim commands:

:[range]CallWithMutableRange {name}([arguments])
                        Call a function (that does not need to accept a range)
                        once for every line in [range].

:[range]NormalWithMutableRange[!] {commands}
                        Execute Normal mode commands {commands} for each line
                        in the [range].

:[range]ExecuteWithMutableRange {expr1} ...
                        Execute the string that results from the evaluation of
                        {expr1} as an Ex command, once for every line in
                        [range]. {expr1} is re-evaluated on each line.
                        Normally, (custom) commands that can operate over
                        multiple lines should take an optional [range], but
                        sometimes this wasn't implemented, and the command
                        only operates on the current position. In these cases,
                        (in addition to ad-hoc expressions) this command is
                        useful, and also handles (most) deletions and
                        insertions gracefully.

For each iteration, the cursor is positioned in the first column of that line.
Folding is temporarily disabled. The cursor is left at the last line (possibly
moved by the last invocation). The arguments are re-evaluated for each line.
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 CommandWithMutableRange.vba.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.

By default, the commands try to find 3 unused marks in the current buffer, and
will refuse to work if no unused marks can be found. Alternatively, you can
reserve any number of marks (but a maximum of 3 will be used) for use by the
commands by setting a global variable (either temporarily in a user function,
or permanently in your vimrc file):
    let g:CommandWithMutableRange_marks = 'abc'

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
CommandWithMutableRange.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.