vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

usr_26.txt (8259B)


      1 *usr_26.txt*	For Vim version 9.2.  Last change: 2026 Feb 14
      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    Hint: set the 'hlsearch' option to see the matches you are going to change,
     70 then you can look ahead and do it faster.
     71 
     72 Adding more than one can be done by prepending the number to CTRL-A.  Suppose
     73 you have this list:
     74 
     75 	1.  item four ~
     76 	2.  item five ~
     77 	3.  item six ~
     78 
     79 Move the cursor to "1." and type: >
     80 
     81 	3 CTRL-A
     82 
     83 The "1." will change to "4.".  Again, you can use "." to repeat this on the
     84 other numbers.
     85 
     86 Another example:
     87 
     88 	006	foo bar ~
     89 	007	foo bar ~
     90 
     91 Using CTRL-A on these numbers results in:
     92 
     93 	007	foo bar ~
     94 	010	foo bar ~
     95 
     96 7 plus one is 10?  What happened here is that Vim recognized "007" as an octal
     97 number, because there is a leading zero.  This notation is often used in C
     98 programs.  If you do not want a number with leading zeros to be handled as
     99 octal, use this: >
    100 
    101 	:set nrformats-=octal
    102 
    103 The CTRL-X command does subtraction in a similar way.
    104 
    105 ==============================================================================
    106 *26.3*	Making a change in many files
    107 
    108 Suppose you have a variable called "x_cnt" and you want to change it to
    109 "x_counter".  This variable is used in several of your C files.  You need to
    110 change it in all files.  This is how you do it.
    111    Put all the relevant files in the argument list: >
    112 
    113 	:args *.c
    114 <
    115 This finds all C files and edits the first one.  Now you can perform a
    116 substitution command on all these files: >
    117 
    118 	:argdo %s/\<x_cnt\>/x_counter/ge | update
    119 
    120 The ":argdo" command takes an argument that is another command.  That command
    121 will be executed on all files in the argument list.
    122    The "%s" substitute command that follows works on all lines.  It finds the
    123 word "x_cnt" with "\<x_cnt\>".  The "\<" and "\>" are used to match the whole
    124 word only, and not "px_cnt" or "x_cnt2".
    125    The flags for the substitute command include "g" to replace all occurrences
    126 of "x_cnt" in the same line.  The "e" flag is used to avoid an error message
    127 when "x_cnt" does not appear in the file.  Otherwise ":argdo" would abort on
    128 the first file where "x_cnt" was not found.
    129    The "|" separates two commands.  The following "update" command writes the
    130 file only if it was changed.  If no "x_cnt" was changed to "x_counter" nothing
    131 happens.
    132 
    133 There is also the ":windo" command, which executes its argument in all
    134 windows.  And ":bufdo" executes its argument on all buffers.  Be careful with
    135 this, because you might have more files in the buffer list than you think.
    136 Check this with the ":buffers" command (or ":ls").
    137 
    138 ==============================================================================
    139 *26.4*	Using Vim from a shell script
    140 
    141 Suppose you have a lot of files in which you need to change the string
    142 "-person-" to "Jones" and then print it.  How do you do that?  One way is to
    143 do a lot of typing.  The other is to write a shell script to do the work.
    144    The Vim editor does a superb job as a screen-oriented editor when using
    145 Normal mode commands.  For batch processing, however, Normal mode commands do
    146 not result in clear, commented command files; so here you will use Ex mode
    147 instead.  This mode gives you a nice command-line interface that makes it easy
    148 to put into a batch file.  ("Ex command" is just another name for a
    149 command-line (:) command.)
    150    The Ex mode commands you need are as follows: >
    151 
    152 	%s/-person-/Jones/g
    153 	write tempfile
    154 	quit
    155 
    156 You put these commands in the file "change.vim".  Now to run the editor in
    157 batch mode, use this shell script: >
    158 
    159 	for file in *.txt; do
    160 	  vim -e -s $file < change.vim
    161 	  lpr -r tempfile
    162 	done
    163 
    164 The for-done loop is a shell construct to repeat the two lines in between,
    165 while the $file variable is set to a different file name each time.
    166    The second line runs the Vim editor in Ex mode (-e argument) on the file
    167 $file and reads commands from the file "change.vim".  The -s argument tells
    168 Vim to operate in silent mode.  In other words, do not keep outputting the
    169 :prompt, or any other prompt for that matter.
    170    The "lpr -r tempfile" command prints the resulting "tempfile" and deletes
    171 it (that's what the -r argument does).
    172 
    173 
    174 READING FROM STDIN
    175 
    176 Vim can read text on standard input.  Since the normal way is to read commands
    177 there, you must tell Vim to read text instead.  This is done by passing the
    178 "-" argument in place of a file.  Example: >
    179 
    180 	ls | vim -
    181 
    182 This allows you to edit the output of the "ls" command, without first saving
    183 the text in a file.
    184    If you use the standard input to read text from, you can use the "-S"
    185 argument to read a script: >
    186 
    187 	producer | vim -S change.vim -
    188 
    189 
    190 NORMAL MODE SCRIPTS
    191 
    192 If you really want to use Normal mode commands in a script, you can use it
    193 like this: >
    194 
    195 	vim -s script file.txt ...
    196 <
    197 	Note:
    198 	"-s" has a different meaning when it is used without "-e".  Here it
    199 	means to source the "script" as Normal mode commands.  When used with
    200 	"-e" it means to be silent, and doesn't use the next argument as a
    201 	file name.
    202 
    203 The commands in "script" are executed like you typed them.  Don't forget that
    204 a line break is interpreted as pressing <Enter>.  In Normal mode that moves
    205 the cursor to the next line.
    206    To create the script you can edit the script file and type the commands.
    207 You need to imagine what the result would be, which can be a bit difficult.
    208 Another way is to record the commands while you perform them manually.  This
    209 is how you do that: >
    210 
    211 	vim -w script file.txt ...
    212 
    213 All typed keys will be written to "script".  If you make a small mistake you
    214 can just continue and remember to edit the script later.
    215    The "-w" argument appends to an existing script.  That is good when you
    216 want to record the script bit by bit.  If you want to start from scratch and
    217 start all over, use the "-W" argument.  It overwrites any existing file.
    218 
    219 ==============================================================================
    220 
    221 Next chapter: |usr_27.txt|  Search commands and patterns
    222 
    223 Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl: