1*usr_26.txt*	For Vim version 7.3.  Last change: 2006 Apr 24
2
3		     VIM USER MANUAL - by Bram Moolenaar
4
5				  Repeating
6
7
8An editing task is hardly ever unstructured.  A change often needs to be made
9several times.  In this chapter a number of useful ways to repeat a change
10will be explained.
11
12|26.1|	Repeating with Visual mode
13|26.2|	Add and subtract
14|26.3|	Making a change in many files
15|26.4|	Using Vim from a shell script
16
17     Next chapter: |usr_27.txt|  Search commands and patterns
18 Previous chapter: |usr_25.txt|  Editing formatted text
19Table of contents: |usr_toc.txt|
20
21==============================================================================
22*26.1*	Repeating with Visual mode
23
24Visual mode is very handy for making a change in any sequence of lines.  You
25can see the highlighted text, thus you can check if the correct lines are
26changed.  But making the selection takes some typing.  The "gv" command
27selects the same area again.  This allows you to do another operation on the
28same text.
29   Suppose you have some lines where you want to change "2001" to "2002" and
30"2000" to "2001":
31
32	The financial results for 2001 are better ~
33	than for 2000.  The income increased by 50%, ~
34	even though 2001 had more rain than 2000. ~
35			2000		2001 ~
36	income		45,403		66,234 ~
37
38First change "2001" to "2002".  Select the lines in Visual mode, and use: >
39
40	:s/2001/2002/g
41
42Now use "gv" to reselect the same text.  It doesn't matter where the cursor
43is.  Then use ":s/2000/2001/g" to make the second change.
44   Obviously, you can repeat these changes several times.
45
46==============================================================================
47*26.2*	Add and subtract
48
49When repeating the change of one number into another, you often have a fixed
50offset.  In the example above, one was added to each year.  Instead of typing
51a substitute command for each year that appears, the CTRL-A command can be
52used.
53   Using the same text as above, search for a year: >
54
55	/19[0-9][0-9]\|20[0-9][0-9]
56
57Now press CTRL-A.  The year will be increased by one:
58
59	The financial results for 2002 are better ~
60	than for 2000.  The income increased by 50%, ~
61	even though 2001 had more rain than 2000. ~
62			2000		2001 ~
63	income		45,403		66,234 ~
64
65Use "n" to find the next year, and press "." to repeat the CTRL-A ("." is a
66bit quicker to type).  Repeat "n" and "." for all years that appear.
67   Hint: set the 'hlsearch' option to see the matches you are going to change,
68then you can look ahead and do it faster.
69
70Adding more than one can be done by prepending the number to CTRL-A.  Suppose
71you have this list:
72
73	1.  item four ~
74	2.  item five ~
75	3.  item six ~
76
77Move the cursor to "1." and type: >
78
79	3 CTRL-A
80
81The "1." will change to "4.".  Again, you can use "." to repeat this on the
82other numbers.
83
84Another example:
85
86	006	foo bar ~
87	007	foo bar ~
88
89Using CTRL-A on these numbers results in:
90
91	007	foo bar ~
92	010	foo bar ~
93
947 plus one is 10?  What happened here is that Vim recognized "007" as an octal
95number, because there is a leading zero.  This notation is often used in C
96programs.  If you do not want a number with leading zeros to be handled as
97octal, use this: >
98
99	:set nrformats-=octal
100
101The CTRL-X command does subtraction in a similar way.
102
103==============================================================================
104*26.3*	Making a change in many files
105
106Suppose you have a variable called "x_cnt" and you want to change it to
107"x_counter".  This variable is used in several of your C files.  You need to
108change it in all files.  This is how you do it.
109   Put all the relevant files in the argument list: >
110
111	:args *.c
112<
113This finds all C files and edits the first one.  Now you can perform a
114substitution command on all these files: >
115
116	:argdo %s/\<x_cnt\>/x_counter/ge | update
117
118The ":argdo" command takes an argument that is another command.  That command
119will be executed on all files in the argument list.
120   The "%s" substitute command that follows works on all lines.  It finds the
121word "x_cnt" with "\<x_cnt\>".  The "\<" and "\>" are used to match the whole
122word only, and not "px_cnt" or "x_cnt2".
123   The flags for the substitute command include "g" to replace all occurrences
124of "x_cnt" in the same line.  The "e" flag is used to avoid an error message
125when "x_cnt" does not appear in the file.  Otherwise ":argdo" would abort on
126the first file where "x_cnt" was not found.
127   The "|" separates two commands.  The following "update" command writes the
128file only if it was changed.  If no "x_cnt" was changed to "x_counter" nothing
129happens.
130
131There is also the ":windo" command, which executes its argument in all
132windows.  And ":bufdo" executes its argument on all buffers.  Be careful with
133this, because you might have more files in the buffer list than you think.
134Check this with the ":buffers" command (or ":ls").
135
136==============================================================================
137*26.4*	Using Vim from a shell script
138
139Suppose you have a lot of files in which you need to change the string
140"-person-" to "Jones" and then print it.  How do you do that?  One way is to
141do a lot of typing.  The other is to write a shell script to do the work.
142   The Vim editor does a superb job as a screen-oriented editor when using
143Normal mode commands.  For batch processing, however, Normal mode commands do
144not result in clear, commented command files; so here you will use Ex mode
145instead.  This mode gives you a nice command-line interface that makes it easy
146to put into a batch file.  ("Ex command" is just another name for a
147command-line (:) command.)
148   The Ex mode commands you need are as follows: >
149
150	%s/-person-/Jones/g
151	write tempfile
152	quit
153
154You put these commands in the file "change.vim".  Now to run the editor in
155batch mode, use this shell script: >
156
157	for file in *.txt; do
158	  vim -e -s $file < change.vim
159	  lpr -r tempfile
160	done
161
162The for-done loop is a shell construct to repeat the two lines in between,
163while the $file variable is set to a different file name each time.
164   The second line runs the Vim editor in Ex mode (-e argument) on the file
165$file and reads commands from the file "change.vim".  The -s argument tells
166Vim to operate in silent mode.  In other words, do not keep outputting the
167:prompt, or any other prompt for that matter.
168   The "lpr -r tempfile" command prints the resulting "tempfile" and deletes
169it (that's what the -r argument does).
170
171
172READING FROM STDIN
173
174Vim can read text on standard input.  Since the normal way is to read commands
175there, you must tell Vim to read text instead.  This is done by passing the
176"-" argument in place of a file.  Example: >
177
178	ls | vim -
179
180This allows you to edit the output of the "ls" command, without first saving
181the text in a file.
182   If you use the standard input to read text from, you can use the "-S"
183argument to read a script: >
184
185	producer | vim -S change.vim -
186
187
188NORMAL MODE SCRIPTS
189
190If you really want to use Normal mode commands in a script, you can use it
191like this: >
192
193	vim -s script file.txt ...
194<
195	Note:
196	"-s" has a different meaning when it is used without "-e".  Here it
197	means to source the "script" as Normal mode commands.  When used with
198	"-e" it means to be silent, and doesn't use the next argument as a
199	file name.
200
201The commands in "script" are executed like you typed them.  Don't forget that
202a line break is interpreted as pressing <Enter>.  In Normal mode that moves
203the cursor to the next line.
204   To create the script you can edit the script file and type the commands.
205You need to imagine what the result would be, which can be a bit difficult.
206Another way is to record the commands while you perform them manually.  This
207is how you do that: >
208
209	vim -w script file.txt ...
210
211All typed keys will be written to "script".  If you make a small mistake you
212can just continue and remember to edit the script later.
213   The "-w" argument appends to an existing script.  That is good when you
214want to record the script bit by bit.  If you want to start from scratch and
215start all over, use the "-W" argument.  It overwrites any existing file.
216
217==============================================================================
218
219Next chapter: |usr_27.txt|  Search commands and patterns
220
221Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:
222