5294
|
1 *usr_26.txt* For Vim version 7.4. Last change: 2006 Apr 24
|
7
|
2
|
|
3 VIM USER MANUAL - by Bram Moolenaar
|
|
4
|
|
5 Repeating
|
|
6
|
|
7
|
|
8 An editing task is hardly ever unstructured. A change often needs to be made
|
|
9 several times. In this chapter a number of useful ways to repeat a change
|
|
10 will 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
|
|
19 Table of contents: |usr_toc.txt|
|
|
20
|
|
21 ==============================================================================
|
|
22 *26.1* Repeating with Visual mode
|
|
23
|
|
24 Visual mode is very handy for making a change in any sequence of lines. You
|
|
25 can see the highlighted text, thus you can check if the correct lines are
|
|
26 changed. But making the selection takes some typing. The "gv" command
|
|
27 selects the same area again. This allows you to do another operation on the
|
|
28 same 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
|
|
38 First change "2001" to "2002". Select the lines in Visual mode, and use: >
|
|
39
|
|
40 :s/2001/2002/g
|
|
41
|
|
42 Now use "gv" to reselect the same text. It doesn't matter where the cursor
|
|
43 is. 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
|
|
49 When repeating the change of one number into another, you often have a fixed
|
|
50 offset. In the example above, one was added to each year. Instead of typing
|
|
51 a substitute command for each year that appears, the CTRL-A command can be
|
|
52 used.
|
|
53 Using the same text as above, search for a year: >
|
|
54
|
|
55 /19[0-9][0-9]\|20[0-9][0-9]
|
|
56
|
|
57 Now 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
|
|
65 Use "n" to find the next year, and press "." to repeat the CTRL-A ("." is a
|
|
66 bit 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,
|
|
68 then you can look ahead and do it faster.
|
|
69
|
|
70 Adding more than one can be done by prepending the number to CTRL-A. Suppose
|
|
71 you have this list:
|
|
72
|
|
73 1. item four ~
|
|
74 2. item five ~
|
|
75 3. item six ~
|
|
76
|
|
77 Move the cursor to "1." and type: >
|
|
78
|
|
79 3 CTRL-A
|
|
80
|
|
81 The "1." will change to "4.". Again, you can use "." to repeat this on the
|
|
82 other numbers.
|
|
83
|
|
84 Another example:
|
|
85
|
|
86 006 foo bar ~
|
|
87 007 foo bar ~
|
|
88
|
|
89 Using CTRL-A on these numbers results in:
|
|
90
|
|
91 007 foo bar ~
|
|
92 010 foo bar ~
|
|
93
|
|
94 7 plus one is 10? What happened here is that Vim recognized "007" as an octal
|
|
95 number, because there is a leading zero. This notation is often used in C
|
|
96 programs. If you do not want a number with leading zeros to be handled as
|
|
97 octal, use this: >
|
|
98
|
|
99 :set nrformats-=octal
|
|
100
|
|
101 The CTRL-X command does subtraction in a similar way.
|
|
102
|
|
103 ==============================================================================
|
|
104 *26.3* Making a change in many files
|
|
105
|
|
106 Suppose 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
|
|
108 change 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 <
|
|
113 This finds all C files and edits the first one. Now you can perform a
|
|
114 substitution command on all these files: >
|
|
115
|
|
116 :argdo %s/\<x_cnt\>/x_counter/ge | update
|
|
117
|
|
118 The ":argdo" command takes an argument that is another command. That command
|
|
119 will be executed on all files in the argument list.
|
|
120 The "%s" substitute command that follows works on all lines. It finds the
|
|
121 word "x_cnt" with "\<x_cnt\>". The "\<" and "\>" are used to match the whole
|
|
122 word only, and not "px_cnt" or "x_cnt2".
|
|
123 The flags for the substitute command include "g" to replace all occurrences
|
|
124 of "x_cnt" in the same line. The "e" flag is used to avoid an error message
|
|
125 when "x_cnt" does not appear in the file. Otherwise ":argdo" would abort on
|
|
126 the first file where "x_cnt" was not found.
|
|
127 The "|" separates two commands. The following "update" command writes the
|
|
128 file only if it was changed. If no "x_cnt" was changed to "x_counter" nothing
|
|
129 happens.
|
|
130
|
|
131 There is also the ":windo" command, which executes its argument in all
|
|
132 windows. And ":bufdo" executes its argument on all buffers. Be careful with
|
|
133 this, because you might have more files in the buffer list than you think.
|
|
134 Check this with the ":buffers" command (or ":ls").
|
|
135
|
|
136 ==============================================================================
|
|
137 *26.4* Using Vim from a shell script
|
|
138
|
|
139 Suppose 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
|
|
141 do 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
|
|
143 Normal mode commands. For batch processing, however, Normal mode commands do
|
|
144 not result in clear, commented command files; so here you will use Ex mode
|
|
145 instead. This mode gives you a nice command-line interface that makes it easy
|
|
146 to put into a batch file. ("Ex command" is just another name for a
|
|
147 command-line (:) command.)
|
|
148 The Ex mode commands you need are as follows: >
|
|
149
|
|
150 %s/-person-/Jones/g
|
|
151 write tempfile
|
|
152 quit
|
|
153
|
|
154 You put these commands in the file "change.vim". Now to run the editor in
|
|
155 batch 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
|
|
162 The for-done loop is a shell construct to repeat the two lines in between,
|
|
163 while 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
|
|
166 Vim 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
|
|
169 it (that's what the -r argument does).
|
|
170
|
|
171
|
|
172 READING FROM STDIN
|
|
173
|
|
174 Vim can read text on standard input. Since the normal way is to read commands
|
|
175 there, 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
|
|
180 This allows you to edit the output of the "ls" command, without first saving
|
|
181 the text in a file.
|
|
182 If you use the standard input to read text from, you can use the "-S"
|
|
183 argument to read a script: >
|
|
184
|
|
185 producer | vim -S change.vim -
|
|
186
|
|
187
|
|
188 NORMAL MODE SCRIPTS
|
|
189
|
|
190 If you really want to use Normal mode commands in a script, you can use it
|
|
191 like 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
|
|
201 The commands in "script" are executed like you typed them. Don't forget that
|
|
202 a line break is interpreted as pressing <Enter>. In Normal mode that moves
|
|
203 the cursor to the next line.
|
|
204 To create the script you can edit the script file and type the commands.
|
|
205 You need to imagine what the result would be, which can be a bit difficult.
|
|
206 Another way is to record the commands while you perform them manually. This
|
|
207 is how you do that: >
|
|
208
|
|
209 vim -w script file.txt ...
|
|
210
|
|
211 All typed keys will be written to "script". If you make a small mistake you
|
|
212 can just continue and remember to edit the script later.
|
|
213 The "-w" argument appends to an existing script. That is good when you
|
|
214 want to record the script bit by bit. If you want to start from scratch and
|
|
215 start all over, use the "-W" argument. It overwrites any existing file.
|
|
216
|
|
217 ==============================================================================
|
|
218
|
|
219 Next chapter: |usr_27.txt| Search commands and patterns
|
|
220
|
|
221 Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl:
|