usr_26.txt ForVim version 9.1. Last change: 2006 Apr 24 VIM USER MANUAL- byBramMoolenaar RepeatingAn editing taskis hardly ever unstructured.A change often needs to be madeseveral times. In this chaptera number of useful ways to repeata changewill be explained.26.1 Repeating withVisual mode26.2 Add and subtract26.3 Makinga change in many files26.4 Using Vim froma shellscript Next chapter:usr_27.txt Search commands and patterns Previous chapter:usr_25.txt Editing formatted textTable of contents:usr_toc.txt==============================================================================26.1 Repeating withVisual modeVisual modeis very handy for makinga change in any sequence of lines. Youcan see the highlighted text, thus you can check if the correct lines arechanged. But making the selection takes some typing. The "gv" commandselects the same area again. This allows you todo another operation on thesame text. Suppose you have some lines where you want to change "2001" to "2002" and"2000" to "2001":The financial results for 2001 are betterthan for 2000. The income increased by 50%,even though 2001 had more rain than 2000.20002001income45,40366,234First change "2001" to "2002".Select the lines inVisual mode, and use::s/2001/2002/gNow use "gv" to reselect the same text. It doesn't matter where the cursoris. Then use ":s/2000/2001/g" to make the second change. Obviously, you can repeat these changes several times.==============================================================================26.2 Add and subtractWhenrepeating the change of one number into another, you often havea fixedoffset. In the example above, one was added to each year. Instead of typinga substitute command for each year that appears, theCTRL-A command can beused. Using the same textas above, search fora year:/19[0-9][0-9]\|20[0-9][0-9]Now pressCTRL-A. The year will be increased by one:The financial results for 2002 are betterthan for 2000. The income increased by 50%,even though 2001 had more rain than 2000.20002001income45,40366,234Use "n" to find the next year, and press "." to repeat theCTRL-A("."isabit 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 anddoit faster.Adding more than one can be done by prepending the number toCTRL-A. Supposeyou have this list:1. item four2. item five3. item sixMove the cursor to "1." and type:3 CTRL-AThe "1." will change to "4.". Again, you can use "." to repeat this on theother numbers.Another example:006foo bar007foo barUsingCTRL-A on these numbers results in:007foo bar010foo bar7 plus oneis 10? What happened hereis that Vim recognized "007"as anoctalnumber, because thereisa leading zero. Thisnotationis often used inCprograms. If youdo not wanta number with leading zeros to be handledasoctal, use this::set nrformats-=octalTheCTRL-X command does subtraction ina similar way.==============================================================================26.3 Makinga change in many filesSuppose you havea variable called "x_cnt" and you want to changeit to"x_counter". This variableis used in several of yourC files. You need tochangeit in all files. Thisis how youdo it. Put all the relevant files in the argument list::args *.cThis finds allC files and edits the first one. Now you can performasubstitution command on all these files::argdo %s/\<x_cnt\>/x_counter/ge | updateThe ":argdo" command takes an argument thatis another command. That commandwill be executed on all files in the argument list. The "%s" substitute command that follows works on all lines. It finds theword "x_cnt" with "\<x_cnt\>". The "\<" and "\>" are used to match the wholeword only, and not "px_cnt" or "x_cnt2". The flags for the substitute command include "g" to replace all occurrencesof "x_cnt" in the same line. The "e" flagis used to avoid an error messagewhen "x_cnt" does not appear in the file. Otherwise ":argdo" would abort onthe first file where "x_cnt" was not found. The "|" separates two commands. The following "update" command writes thefile only ifit was changed. If no "x_cnt" was changed to "x_counter" nothinghappens.Thereis also the ":windo" command, which executes its argument in allwindows. And ":bufdo" executes its argument on all buffers. Be careful withthis, because you might have more files in the bufferlist than you think.Check this with the ":buffers" command (or ":ls").==============================================================================26.4 Using Vim froma shellscriptSuppose you havea lot of files in which you need to change thestring"-person-" to "Jones" and then print it. Howdo youdo that? One wayis todoa lot of typing. The otheris to writea shellscript todo the work. The Vim editor doesa superbjobasa screen-oriented editor when usingNormal mode commands. For batch processing, however,Normal mode commandsdonot result in clear, commented command files; so here you will useEx modeinstead. This mode gives youa nice command-line interface that makesiteasytoput intoa batch file.("Ex command"is just another name foracommand-line (:) command.) TheEx mode commands you need areas follows:%s/-person-/Jones/gwrite tempfilequitYouput these commands in the file "change.vim". Now to run the editor inbatch mode, use this shell script:for file in *.txt; do vim -e -s $file < change.vim lpr -r tempfiledoneThe for-done loopisa shell construct to repeat the two lines in between,while the $file variableis set toa different file name each time. The second line runs the Vim editor inEx mode (-e argument) on the file$file and reads commands from the file "change.vim". The-s argument tellsVim to operate in silent mode. In other words,do not keep outputting the:prompt, or any other prompt for that matter. The "lpr-rtempfile" command prints the resulting "tempfile" and deletesit (that's what the-r argument does).READING FROM STDINVim can read text on standard input. Since the normal wayis to read commandsthere, youmust tell Vim to read text instead. Thisis done by passing the"-" argument in place ofa file. Example:ls | vim -This allows you to edit the output of the "ls" command, without first savingthe text ina file. If you use the standard input to read text from, you can use the "-S"argument to reada script:producer | vim -S change.vim -NORMAL MODE SCRIPTSIf you really want to useNormal mode commands ina script, you can useitlike this:vim -s script file.txt ...Note:"-s" hasa different meaning whenitis used without "-e". Hereitmeans to source the "script"asNormal mode commands. When used with"-e"it means to be silent, and doesn't use the next argumentasafile name.The commands in "script" are executed like you typed them. Don't forget thata line breakis interpretedas pressing<Enter>. InNormal mode that movesthe cursor to the next line. To create thescript you can edit thescript file and type the commands.You need to imagine what the result would be, which can bea bit difficult.Another wayis to record the commands while you perform them manually. Thisis how youdo that:vim -w script file.txt ...All typed keys will be written to "script". If you makea small mistake youcan just continue and remember to edit thescript later. The "-w" argument appends to an existing script. Thatis good when youwant to record thescript bit by bit. If you want to start from scratch andstart all over, use the "-W" argument. It overwrites any existing file.==============================================================================Next chapter:usr_27.txt Search commands and patternsCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: