usr_26.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. *usr_26.txt* Nvim
  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. Adding more than one can be done by prepending the number to CTRL-A. Suppose
  50. you have this list:
  51. 1. item four ~
  52. 2. item five ~
  53. 3. item six ~
  54. Move the cursor to "1." and type: >
  55. 3 CTRL-A
  56. The "1." will change to "4.". Again, you can use "." to repeat this on the
  57. other numbers.
  58. The CTRL-X command does subtraction in a similar way.
  59. The behavior of CTRL-A and CTRL-X depends on the value of |'nrformats'|. For
  60. example, if you use: >
  61. :set nrformats+=octal
  62. pressing CTRL-A over "007" will increment to "010", because "007" will be
  63. identified as an octal number.
  64. ==============================================================================
  65. *26.3* Making a change in many files
  66. Suppose you have a variable called "x_cnt" and you want to change it to
  67. "x_counter". This variable is used in several of your C files. You need to
  68. change it in all files. This is how you do it.
  69. Put all the relevant files in the argument list: >
  70. :args *.c
  71. <
  72. This finds all C files and edits the first one. Now you can perform a
  73. substitution command on all these files: >
  74. :argdo %s/\<x_cnt\>/x_counter/ge | update
  75. The ":argdo" command takes an argument that is another command. That command
  76. will be executed on all files in the argument list.
  77. The "%s" substitute command that follows works on all lines. It finds the
  78. word "x_cnt" with "\<x_cnt\>". The "\<" and "\>" are used to match the whole
  79. word only, and not "px_cnt" or "x_cnt2".
  80. The flags for the substitute command include "g" to replace all occurrences
  81. of "x_cnt" in the same line. The "e" flag is used to avoid an error message
  82. when "x_cnt" does not appear in the file. Otherwise ":argdo" would abort on
  83. the first file where "x_cnt" was not found.
  84. The "|" separates two commands. The following "update" command writes the
  85. file only if it was changed. If no "x_cnt" was changed to "x_counter" nothing
  86. happens.
  87. There is also the ":windo" command, which executes its argument in all
  88. windows. And ":bufdo" executes its argument on all buffers. Be careful with
  89. this, because you might have more files in the buffer list than you think.
  90. Check this with the ":buffers" command (or ":ls").
  91. ==============================================================================
  92. *26.4* Using Vim from a shell script
  93. Suppose you have a lot of files in which you need to change the string
  94. "-person-" to "Jones" and then print it. How do you do that? One way is to
  95. do a lot of typing. The other is to write a shell script to do the work.
  96. The Vim editor does a superb job as a screen-oriented editor when using
  97. Normal mode commands. For batch processing, however, Normal mode commands do
  98. not result in clear, commented command files; so here you will use Ex mode
  99. instead. This mode gives you a nice command-line interface that makes it easy
  100. to put into a batch file. ("Ex command" is just another name for a
  101. command-line (:) command.)
  102. The Ex mode commands you need are as follows: >
  103. %s/-person-/Jones/g
  104. write tempfile
  105. quit
  106. You put these commands in the file "change.vim". Now to run the editor in
  107. batch mode, use this shell script: >
  108. for file in *.txt; do
  109. vim -e -s $file < change.vim
  110. lpr -r tempfile
  111. done
  112. The for-done loop is a shell construct to repeat the two lines in between,
  113. while the $file variable is set to a different file name each time.
  114. The second line runs the Vim editor in Ex mode (-e argument) on the file
  115. $file and reads commands from the file "change.vim". The -s argument tells
  116. Vim to operate in silent mode. In other words, do not keep outputting the
  117. :prompt, or any other prompt for that matter.
  118. The "lpr -r tempfile" command prints the resulting "tempfile" and deletes
  119. it (that's what the -r argument does).
  120. READING FROM STDIN
  121. Vim can read text on standard input. Since the normal way is to read commands
  122. there, you must tell Vim to read text instead. This is done by passing the
  123. "-" argument in place of a file. Example: >
  124. ls | vim -
  125. This allows you to edit the output of the "ls" command, without first saving
  126. the text in a file.
  127. If you use the standard input to read text from, you can use the "-S"
  128. argument to read a script: >
  129. producer | vim -S change.vim -
  130. NORMAL MODE SCRIPTS
  131. If you really want to use Normal mode commands in a script, you can use it
  132. like this: >
  133. vim -s script file.txt ...
  134. <
  135. Note:
  136. "-s" has a different meaning when it is used without "-e". Here it
  137. means to source the "script" as Normal mode commands. When used with
  138. "-e" it means to be silent, and doesn't use the next argument as a
  139. file name.
  140. The commands in "script" are executed like you typed them. Don't forget that
  141. a line break is interpreted as pressing <Enter>. In Normal mode that moves
  142. the cursor to the next line.
  143. To create the script you can edit the script file and type the commands.
  144. You need to imagine what the result would be, which can be a bit difficult.
  145. Another way is to record the commands while you perform them manually. This
  146. is how you do that: >
  147. vim -w script file.txt ...
  148. All typed keys will be written to "script". If you make a small mistake you
  149. can just continue and remember to edit the script later.
  150. The "-w" argument appends to an existing script. That is good when you
  151. want to record the script bit by bit. If you want to start from scratch and
  152. start all over, use the "-W" argument. It overwrites any existing file.
  153. ==============================================================================
  154. Next chapter: |usr_27.txt| Search commands and patterns
  155. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: