robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

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