*repeat.txt*    For Vim version 6.0j.  Last change: 2000 Oct 18


		  VIM REFERENCE MANUAL    by Bram Moolenaar


Repeating commands					*repeating*

1. Single repeats	|single-repeat|
2. Multiple repeats	|multi-repeat|
3. Complex repeats	|complex-repeat|

==============================================================================
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'.

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.  If the last change included a specification of a numbered register,
the register number will be incremented.  See |undo-redo| 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.


==============================================================================
2. Multiple repeats					*multi-repeat*

							*:g* *:global*
:[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!.

The global commands work by first scanning through the [range] lines and
marking each line where a match occurs.  In a second scan the [cmd] is
executed for each marked line with its line number prepended.  If a line is
changed or 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 matching line.

To repeat a non-Ex 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 easy to globally
replace a string:
	:g/pat/s//PAT/g
This replaces all occurrences of "pat" with "PAT".  The same can be done with:
	:%s/pat/PAT/g
Which is two characters shorter!

==============================================================================
3. Complex repeats					*complex-repeat*

							*q* *recording*
q{0-9a-zA-Z"}		Record typed characters into register {0-9a-zA-Z"}
			(uppercase to append).  The 'q' command is disabled
			while executing a register, and it doesn't work inside
			a mapping.  {Vi: no recording}

q			Stops recording.  (Implementation note: The 'q' that
			stops recording is not stored in the register, unless
			it was the result of a mapping)  {Vi: no recording}

							*@*
@{0-9a-z".=*}		Execute the contents of register {0-9a-z".=*} [count]
			times.  Note that register '%' (name of the current
			file) and '#' (name of the alternate file) cannot be
			used.  For "@=" you are prompted to enter an
			expression.  The result of the expression is then
			executed.  See also |@:|.  {Vi: only named registers}

							*@@*
@@			Repeat the previous @{0-9a-z":*} [count] times.

:[addr]*{0-9a-z".=}						*:@* *:star*
:[addr]@{0-9a-z".=*}	Execute the contents of register {0-9a-z".=*} as an Ex
			command.  First set cursor at line [addr] (default is
			current line).  When the last line in the register does
			not have a <CR> it will be added automatically when
			the 'e' flag is present in 'cpoptions'.
			Note that the ":*" command is only recognized when the
			'*' flag is present in 'cpoptions'.  This is NOT the
			default when 'nocompatible' is used.
			For ":@=" the last used expression is used.  The
			result of evaluating the expression is executed as an
			Ex command.
			Mappings are not recognized in these commands.
			{Vi: only in some versions} Future: Will execute the
			register for each line in the address range.

							*:@:*
:[addr]@:		Repeat last command-line.  First set cursor at line
			[addr] (default is current line).  {not in Vi}

							*:@@*
:[addr]@@		Repeat the previous :@{0-9a-z"}.  First set cursor at
			line [addr] (default is current line).  {Vi: only in
			some versions}

					*:so* *:source* *load-vim-script*
:so[urce] {file}	Read Ex commands from {file}.

:so[urce]! {file}	Read Vim commands from {file}.  {not in Vi}

							*:ru* *:runtime*
:ru[ntime][!] {file}	Read Ex commands from {file} in each directory given
			by 'runtimepath'.  There is no error for non-existing
			files.  Example: >
				:runtime syntax/c.vim
<			When [!] is included, all found files are sourced.
			When it is not included only the first found file is
			sourced.
			When {file} contains wildcards it is expanded to all
			matching files.  Example: >
				:runtime! plugin/*.vim
<			This is what Vim uses to load the plugin files when
			starting up. This similar command: >
				:runtime plugin/*.vim
<			will source the first file only.
			Only one file name can be given, spaces are considered
			to be part of the name, except at the start and end.
			{not in Vi}

						*:scrip* *:scriptnames*
:scrip[tnames]		List all sourced script names, in the order they were
			first sourced.  The number is used for script ID
			|<SID>|.
			{not in Vi} {not available when compiled without the
			|+eval| feature}

							*:fini* *:finish*
:fini[sh]		Stop sourcing a script.  Can only be used in a Vim
			script file.  This is a quick way to skip the rest of
			the file.
			{not in Vi}

All commands and command sequences can be repeated by putting them in a named
register and then executing it.  There are two ways to get the commands in the
register:
- Use the record command "q".  You type the commands once, and while they are
  being executed they are stored in a register.  Easy, because you can see
  what you are doing.  If you make a mistake, "p"ut the register into the
  file, edit the command sequence, and then delete it into the register
  again.  You can continue recording by appending to the register (use an
  uppercase letter).
- Delete or yank the command sequence into the register.

Often used command sequences can be put under a function key with the ':map'
command.

An alternative is to put the commands in a file, and execute them with the
':source!' command.  Useful for long command sequences.  Can be combined with
the ':map' command to put complicated commands under a function key.

The ':source' command reads Ex commands from a file line by line.  You will
have to type any needed keyboard input.  The ':source!' command reads from a
script file character by character, interpreting each character as if you
typed it.

Example: When you give the ":!ls" command you get the |hit-return| prompt.  If
you ':source' a file with the line "!ls" in it, you will have to type the
return yourself.  But if you ':source!' a file with the line ":!ls" in it, the
next characters from that file are read until a <CR> is found.  You will not
have to type <CR> yourself, unless ":!ls" was the last line in the file.

It is possible to put ':source[!]' commands in the script file, so you can
make a top-down hierarchy of script files.  The ':source' command can be
nested as deep as the number of files that can be opened at one time (about
15).  The ':source!' command can be nested up to 15 levels deep.

You can use the "<sfile>" string (literally, this is not a special key) inside
of the sourced file, in places where a file name is expected.  It will be
replaced by the file name of the sourced file.  For example, if you have a
"other.vimrc" file in the same directory as your ".vimrc" file, you can source
it from your ".vimrc" file with this command: >
	:source <sfile>:h/other.vimrc

In script files terminal-dependent key codes are represented by
terminal-independent two character codes.  This means that they can be used
in the same way on different kinds of terminals.  The first character of a
key code is 0x80 or 128, shown on the screen as "~@".  The second one can be
found in the list |key-notation|.  Any of these codes can also be entered
with CTRL-V followed by the three digit decimal code.  This does NOT work for
the <t_xx> termcap codes, these can only be used in mappings.

							*:source_crnl*
MS-DOS, Win32 and OS/2: Files that are read with ":source" normally have
<CR><NL> <EOL>s.  These always work.  If you are using a file with <NL> <EOL>s
(for example, a file made on Unix), this will be recognized if 'fileformats'
is not empty and the first line does not end in a <CR>.  This fails if the
first line has something like ":map <F1> :help^M", where "^M" is a <CR>.  If
the first line ends in a <CR>, but following ones don't, you will get an error
message, because the <CR> from the first lines will be lost.

On other systems, Vim expects ":source"ed files to end in a <NL>.  These
always work.  If you are using a file with <CR><NL> <EOL>s (for example, a
file made on MS-DOS), all lines will have a trailing <CR>.  This may cause
problems for some commands (e.g., mappings).  There is no automatic <EOL>
detection, because it's common to start with a line that defines a mapping
that ends in a <CR>, which will confuse the automaton.

							*line-continuation*
Long lines in a ":source"d Ex command script file can be split by inserting
a line continuation symbol "\" (backslash) at the start of the next line.
There can be white space before the backslash, which is ignored.

Example: the lines >
	:set comments=sr:/*,mb:*,el:*/,
		     \://,
		     \b:#,
		     \:%,
		     \n:>,
		     \fb:-
are interpreted as if they were given in one line:
	:set comments=sr:/*,mb:*,el:*/,://,b:#,:%,n:>,fb:-

All leading whitespace characters in the line before a backslash are ignored.
Note however that trailing whitespace in the line before it cannot be
inserted freely; it depends on the position where a command is split up
whether additional whitespace is allowed or not.

There is a problem with the ":append" and ":insert" commands: >
   :1append
   \asdf
   .
The backslash is seen as a line-continuation symbol, thus this results in the
command: >
   :1appendasdf
   .
To avoid this, add the 'C' flag to the 'cpoptions' option: >
   :set cpo+=C
   :1append
   \asdf
   .
   :set cpo-=C

Rationale: Most programs work with a trailing backslash to indicate line
continuation.  Using this in Vim would cause incompatibility with Vi.  For
example for this Vi mapping: >
	:map xx  asdf\
Therefore the unusual leading backslash is used.

 vim:tw=78:
