*repeat.txt* For Vim version 9.1. Last change: 2024 Jul 11
VIM REFERENCE MANUAL by Bram Moolenaar
Repeating commands, Vim scripts and debugging *repeating*
Chapter 26 of the user manual introduces repeating |usr_26.txt|.
1. Single repeats |single-repeat|
2. Multiple repeats |multi-repeat|
3. Complex repeats |complex-repeat|
4. Using Vim scripts |using-scripts|
5. Using Vim packages |packages|
6. Creating Vim packages |package-create|
7. Debugging scripts |debug-scripts|
8. Profiling |profiling|
==============================================================================
1. Single repeats *single-repeat*
*.*
. Repeat last change, with count replaced with [count].
Also repeat a yank command, when the 'y' flag is
included in 'cpoptions'. Does not repeat a
command-line command.
Simple changes can be repeated with the "." command. Without a count, the
count of the last change is used. If you enter a count, it will replace the
last one. |v:count| and |v:count1| will be set.
If the last change included a specification of a numbered register, the
register number will be incremented. See |redo-register| for an example how
to use this.
Note that when repeating a command that used a Visual selection, the same SIZE
of area is used, see |visual-repeat|.
*@:*
@: Repeat last command-line [count] times.
{not available when compiled without the
|+cmdline_hist| feature}
==============================================================================
2. Multiple repeats *multi-repeat*
*:g* *:global* *E148*
:[range]g[lobal]/{pattern}/[cmd]
Execute the Ex command [cmd] (default ":p") on the
lines within [range] where {pattern} matches.
:[range]g[lobal]!/{pattern}/[cmd]
Execute the Ex command [cmd] (default ":p") on the
lines within [range] where {pattern} does NOT match.
*:v* *:vglobal*
:[range]v[global]/{pattern}/[cmd]
Same as :g!.
Example: >
:g/^Obsolete/d _
Using the underscore after `:d` avoids clobbering registers or the clipboard.
This also makes it faster.
Instead of the '/' which surrounds the {pattern}, you can use any other
single byte character, but not an alphabetic character, '\', '"', '|' or '!'.
This is useful if you want to include a '/' in the search pattern or
replacement string.
For the definition of a pattern, see |pattern|.
NOTE [cmd] may contain a range; see |collapse| and |edit-paragraph-join| for
examples.
The global commands work by first scanning through the [range] lines and
marking each line where a match occurs (for a multi-line pattern, only the
start of the match matters).
In a second scan the [cmd] is executed for each marked line, as if the cursor
was in that line. For ":v" and ":g!" the command is executed for each not
marked line. If a line is deleted its mark disappears.
The default for [range] is the whole buffer (1,$). Use "CTRL-C" to interrupt
the command. If an error message is given for a line, the command for that
line is aborted and the global command continues with the next marked or
unmarked line.
*E147*
When the command is used recursively, it only works on one line. Giving a
range is then not allowed. This is useful to find all lines that match a
pattern and do not match another pattern: >
:g/found/v/notfound/{cmd}
This first finds all lines containing "found", but only executes {cmd} when
there is no match for "notfound".
Any Ex command can be used, see |ex-cmd-index|. To execute a Normal mode
command, you can use the `:normal` command: >
:g/pat/normal {commands}
Make sure that {commands} ends with a whole command, otherwise Vim will wait
for you to type the rest of the command for each match. The screen will not
have been updated, so you don't know what you are doing. See |:normal|.
The undo/redo command will undo/redo the whole global command at once.
The previous context mark will only be set once (with "''" you go back to
where the cursor was before the global command).
The global command sets both the last used search pattern and the last used
substitute pattern (this is vi compatible). This makes it eas