Quick links: help overview · quick reference · user manual toc · reference manual toc · faq
Go to keyword (shortcut: k)
Site search (shortcut: s)
usr_26.txt  	For Vim version 9.1.  Last change: 2025 Nov 09


		     VIM USER MANUAL	by Bram Moolenaar


				  Repeating


An editing task is hardly ever unstructured.  A change often needs to be made
several times.  In this chapter a number of useful ways to repeat a change
will be explained.

26.1  	Repeating with Visual mode
26.2  	Add and subtract
26.3  	Making a change in many files
26.4  	Using Vim from a shell script

     Next chapter: usr_27.txt  Search commands and patterns
 Previous chapter: usr_25.txt  Editing formatted text
Table of contents: usr_toc.txt

==============================================================================
26.1  	Repeating with Visual mode

Visual mode is very handy for making a change in any sequence of lines.  You
can see the highlighted text, thus you can check if the correct lines are
changed.  But making the selection takes some typing.  The "gv" command
selects the same area again.  This allows you to do another operation on the
same text.
   Suppose you have some lines where you want to change "2001" to "2002" and
"2000" to "2001":

	The financial results for 2001 are better 
	than for 2000.  The income increased by 50%, 
	even though 2001 had more rain than 2000. 
			2000		2001 
	income		45,403		66,234 

First change "2001" to "2002".  Select the lines in Visual mode, and use: 

	:s/2001/2002/g

Now use "gv" to reselect the same text.  It doesn't matter where the cursor
is.  Then use ":s/2000/2001/g" to make the second change.
   Obviously, you can repeat these changes several times.

==============================================================================
26.2  	Add and subtract

When repeating the change of one number into another, you often have a fixed
offset.  In the example above, one was added to each year.  Instead of typing
a substitute command for each year that appears, the CTRL-A command can be
used.
   Using the same text as above, search for a year: 

	/19[0-9][0-9]\|20[0-9][0-9]

Now press CTRL-A.  The year will be increased by one:

	The financial results for 2002 are better 
	than for 2000.  The income increased by 50%, 
	even though 2001 had more rain than 2000. 
			2000		2001 
	income		45,403		66,234 

Use "n" to find the next year, and press "." to repeat the CTRL-A ("." is a
bit quicker to type).  Repeat "n" and "." for all years that appear.
   Hint: set the 'hlsearch' option to see the matches you are going to change,
then you can look ahead and do it faster.

Adding more than one can be done by prepending the number to CTRL-A.  Suppose
you have this list:

	1.  item four 
	2.  item five 
	3.  item six 

Move the cursor to "1." and type: 

	3 CTRL-A

The "1." will change to "4.".  Again, you can use "." to repeat this on the
other numbers.

Another example:

	006	foo bar 
	007	foo bar 

Using CTRL-A on these numbers results in:

	007	foo bar 
	010	foo bar 

7 plus one is 10?  What happened here is that Vim recognized "007" as an octal
number, because there is a leading zero.  This notation is often used in C
programs.  If you do not want a number with leading zeros to be handled as
octal, use this: 

	:set nrformats-=octal

The CTRL-X command does subtraction in a similar way.

==============================================================================
26.3  	Making a change in many files

Suppose you have a variable called "x_cnt" and you want to change it to
"x_counter".  This variable is used in several of your C files.  You need to
change it in all files.  This is how you do it.
   Put all the relevant files in the argument list: 

	:args *.c

This finds all C files and edits the first one.  Now you can perform a
substitution command on all these files: 

	:argdo %s/\<x_cnt\>/x_counter/ge | update

The ":argdo" command takes an argument that is another command.  That command
will be executed on all files in the argument list.
   The "%s" substitute command that follows works on all lines.  It finds the
word "x_cnt" with "\<x_cnt\>".  The "\<" and "\>" are used to match the whole
word only, and not "px_cnt" or "x_cnt2".
   The flags for the substitute command include "g" to replace all occurrences
of "x_cnt" in the same line.  The "e" flag is used to avoid an error message
when "x_cnt" does not appear in the file.  Otherwise ":argdo" would abort on
the first file where "x_cnt" was not found.
   The "|" separates two commands.  The following "update" command writes the
file only if