usr_26.txt 8.1 KB

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