usr_26.txt (7936B)
1 *usr_26.txt* Nvim 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 Repeating 8 9 10 An editing task is hardly ever unstructured. A change often needs to be made 11 several times. In this chapter a number of useful ways to repeat a change 12 will 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 21 Table of contents: |usr_toc.txt| 22 23 ============================================================================== 24 *26.1* Repeating with Visual mode 25 26 Visual mode is very handy for making a change in any sequence of lines. You 27 can see the highlighted text, thus you can check if the correct lines are 28 changed. But making the selection takes some typing. The "gv" command 29 selects the same area again. This allows you to do another operation on the 30 same 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 40 First change "2001" to "2002". Select the lines in Visual mode, and use: > 41 42 :s/2001/2002/g 43 44 Now use "gv" to reselect the same text. It doesn't matter where the cursor 45 is. 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 51 When repeating the change of one number into another, you often have a fixed 52 offset. In the example above, one was added to each year. Instead of typing 53 a substitute command for each year that appears, the CTRL-A command can be 54 used. 55 Using the same text as above, search for a year: > 56 57 /19[0-9][0-9]\|20[0-9][0-9] 58 59 Now 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 67 Use "n" to find the next year, and press "." to repeat the CTRL-A ("." is a 68 bit quicker to type). Repeat "n" and "." for all years that appear. 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 The CTRL-X command does subtraction in a similar way. 85 86 The behavior of CTRL-A and CTRL-X depends on the value of 'nrformats'. For 87 example, if you use: > 88 89 :set nrformats+=octal 90 91 pressing CTRL-A over "007" will increment to "010", because "007" will be 92 identified as an octal number. 93 94 ============================================================================== 95 *26.3* Making a change in many files 96 97 Suppose 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 99 change 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 < 104 This finds all C files and edits the first one. Now you can perform a 105 substitution command on all these files: > 106 107 :argdo %s/\<x_cnt\>/x_counter/ge | update 108 109 The ":argdo" command takes an argument that is another command. That command 110 will be executed on all files in the argument list. 111 The "%s" substitute command that follows works on all lines. It finds the 112 word "x_cnt" with "\<x_cnt\>". The "\<" and "\>" are used to match the whole 113 word only, and not "px_cnt" or "x_cnt2". 114 The flags for the substitute command include "g" to replace all occurrences 115 of "x_cnt" in the same line. The "e" flag is used to avoid an error message 116 when "x_cnt" does not appear in the file. Otherwise ":argdo" would abort on 117 the first file where "x_cnt" was not found. 118 The "|" separates two commands. The following "update" command writes the 119 file only if it was changed. If no "x_cnt" was changed to "x_counter" nothing 120 happens. 121 122 There is also the ":windo" command, which executes its argument in all 123 windows. And ":bufdo" executes its argument on all buffers. Be careful with 124 this, because you might have more files in the buffer list than you think. 125 Check this with the ":buffers" command (or ":ls"). 126 127 ============================================================================== 128 *26.4* Using Vim from a shell script 129 130 Suppose 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 132 do 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 134 Normal mode commands. For batch processing, however, Normal mode commands do 135 not result in clear, commented command files; so here you will use Ex mode 136 instead. This mode gives you a nice command-line interface that makes it easy 137 to put into a batch file. ("Ex command" is just another name for a 138 command-line (:) command.) 139 The Ex mode commands you need are as follows: > 140 141 %s/-person-/Jones/g 142 write tempfile 143 quit 144 145 You put these commands in the file "change.vim". Now to run the editor in 146 batch 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 153 The for-done loop is a shell construct to repeat the two lines in between, 154 while 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 157 Vim 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 160 it (that's what the -r argument does). 161 162 163 READING FROM STDIN 164 165 Vim can read text on standard input. Since the normal way is to read commands 166 there, 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 171 This allows you to edit the output of the "ls" command, without first saving 172 the text in a file. 173 If you use the standard input to read text from, you can use the "-S" 174 argument to read a script: > 175 176 producer | vim -S change.vim - 177 178 179 NORMAL MODE SCRIPTS 180 181 If you really want to use Normal mode commands in a script, you can use it 182 like 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 192 The commands in "script" are executed like you typed them. Don't forget that 193 a line break is interpreted as pressing <Enter>. In Normal mode that moves 194 the cursor to the next line. 195 To create the script you can edit the script file and type the commands. 196 You need to imagine what the result would be, which can be a bit difficult. 197 Another way is to record the commands while you perform them manually. This 198 is how you do that: > 199 200 vim -w script file.txt ... 201 202 All typed keys will be written to "script". If you make a small mistake you 203 can just continue and remember to edit the script later. 204 The "-w" argument appends to an existing script. That is good when you 205 want to record the script bit by bit. If you want to start from scratch and 206 start all over, use the "-W" argument. It overwrites any existing file. 207 208 ============================================================================== 209 210 Next chapter: |usr_27.txt| Search commands and patterns 211 212 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: