usr_12.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. *usr_12.txt* For Vim version 9.0. Last change: 2022 Nov 19
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Clever tricks
  4. By combining several commands you can make Vim do nearly everything. In this
  5. chapter a number of useful combinations will be presented. This uses the
  6. commands introduced in the previous chapters and a few more.
  7. |12.1| Replace a word
  8. |12.2| Change "Last, First" to "First Last"
  9. |12.3| Sort a list
  10. |12.4| Reverse line order
  11. |12.5| Count words
  12. |12.6| Find a man page
  13. |12.7| Trim blanks
  14. |12.8| Find where a word is used
  15. Next chapter: |usr_20.txt| Typing command-line commands quickly
  16. Previous chapter: |usr_11.txt| Recovering from a crash
  17. Table of contents: |usr_toc.txt|
  18. ==============================================================================
  19. *12.1* Replace a word
  20. The substitute command can be used to replace all occurrences of a word with
  21. another word: >
  22. :%s/four/4/g
  23. The "%" range means to replace in all lines. The "g" flag at the end causes
  24. all words in a line to be replaced.
  25. This will not do the right thing if your file also contains "thirtyfour".
  26. It would be replaced with "thirty4". To avoid this, use the "\<" item to
  27. match the start of a word: >
  28. :%s/\<four/4/g
  29. Obviously, this still goes wrong on "fourteen". Use "\>" to match the end of
  30. a word: >
  31. :%s/\<four\>/4/g
  32. If you are programming, you might want to replace "four" in comments, but not
  33. in the code. Since this is difficult to specify, add the "c" flag to have the
  34. substitute command prompt you for each replacement: >
  35. :%s/\<four\>/4/gc
  36. REPLACING IN SEVERAL FILES
  37. Suppose you want to replace a word in more than one file. You could edit each
  38. file and type the command manually. It's a lot faster to use record and
  39. playback.
  40. Let's assume you have a directory with C++ files, all ending in ".cpp".
  41. There is a function called "GetResp" that you want to rename to "GetAnswer".
  42. vim *.cpp Start Vim, defining the argument list to
  43. contain all the C++ files. You are now in the
  44. first file.
  45. qq Start recording into the q register
  46. :%s/\<GetResp\>/GetAnswer/g
  47. Do the replacements in the first file.
  48. :wnext Write this file and move to the next one.
  49. q Stop recording.
  50. @q Execute the q register. This will replay the
  51. substitution and ":wnext". You can verify
  52. that this doesn't produce an error message.
  53. 999@q Execute the q register on the remaining files.
  54. At the last file you will get an error message, because ":wnext" cannot move
  55. to the next file. This stops the execution, and everything is done.
  56. Note:
  57. When playing back a recorded sequence, an error stops the execution.
  58. Therefore, make sure you don't get an error message when recording.
  59. There is one catch: If one of the .cpp files does not contain the word
  60. "GetResp", you will get an error and replacing will stop. To avoid this, add
  61. the "e" flag to the substitute command: >
  62. :%s/\<GetResp\>/GetAnswer/ge
  63. The "e" flag tells ":substitute" that not finding a match is not an error.
  64. ==============================================================================
  65. *12.2* Change "Last, First" to "First Last"
  66. You have a list of names in this form:
  67. Doe, John ~
  68. Smith, Peter ~
  69. You want to change that to:
  70. John Doe ~
  71. Peter Smith ~
  72. This can be done with just one command: >
  73. :%s/\([^,]*\), \(.*\)/\2 \1/
  74. Let's break this down in parts. Obviously it starts with a substitute
  75. command. The "%" is the line range, which stands for the whole file. Thus
  76. the substitution is done in every line in the file.
  77. The arguments for the substitute command are "/from/to/". The slashes
  78. separate the "from" pattern and the "to" string. This is what the "from"
  79. pattern contains:
  80. \([^,]*\), \(.*\) ~
  81. The first part between \( \) matches "Last" \( \)
  82. match anything but a comma [^,]
  83. any number of times *
  84. matches ", " literally ,
  85. The second part between \( \) matches "First" \( \)
  86. any character .
  87. any number of times *
  88. In the "to" part we have "\2" and "\1". These are called backreferences.
  89. They refer to the text matched by the "\( \)" parts in the pattern. "\2"
  90. refers to the text matched by the second "\( \)", which is the "First" name.
  91. "\1" refers to the first "\( \)", which is the "Last" name.
  92. You can use up to nine backreferences in the "to" part of a substitute
  93. command. "\0" stands for the whole matched pattern. There are a few more
  94. special items in a substitute command, see |sub-replace-special|.
  95. ==============================================================================
  96. *12.3* Sort a list
  97. In a Makefile you often have a list of files. For example:
  98. OBJS = \ ~
  99. version.o \ ~
  100. pch.o \ ~
  101. getopt.o \ ~
  102. util.o \ ~
  103. getopt1.o \ ~
  104. inp.o \ ~
  105. patch.o \ ~
  106. backup.o ~
  107. To sort this list, filter the text through the external sort command: >
  108. /^OBJS
  109. j
  110. :.,/^$/-1!sort
  111. This goes to the first line, where "OBJS" is the first thing in the line.
  112. Then it goes one line down and filters the lines until the next empty line.
  113. You could also select the lines in Visual mode and then use "!sort". That's
  114. easier to type, but more work when there are many lines.
  115. The result is this:
  116. OBJS = \ ~
  117. backup.o ~
  118. getopt.o \ ~
  119. getopt1.o \ ~
  120. inp.o \ ~
  121. patch.o \ ~
  122. pch.o \ ~
  123. util.o \ ~
  124. version.o \ ~
  125. Notice that a backslash at the end of each line is used to indicate the line
  126. continues. After sorting, this is wrong! The "backup.o" line that was at
  127. the end didn't have a backslash. Now that it sorts to another place, it
  128. must have a backslash.
  129. The simplest solution is to add the backslash with "A \<Esc>". You can
  130. keep the backslash in the last line, if you make sure an empty line comes
  131. after it. That way you don't have this problem again.
  132. ==============================================================================
  133. *12.4* Reverse line order
  134. The |:global| command can be combined with the |:move| command to move all the
  135. lines before the first line, resulting in a reversed file. The command is: >
  136. :global/^/move 0
  137. Abbreviated: >
  138. :g/^/m 0
  139. The "^" regular expression matches the beginning of the line (even if the line
  140. is blank). The |:move| command moves the matching line to after the imaginary
  141. zeroth line, so the current matching line becomes the first line of the file.
  142. As the |:global| command is not confused by the changing line numbering,
  143. |:global| proceeds to match all remaining lines of the file and puts each as
  144. the first.
  145. This also works on a range of lines. First move to above the first line and
  146. mark it with "mt". Then move the cursor to the last line in the range and
  147. type: >
  148. :'t+1,.g/^/m 't
  149. ==============================================================================
  150. *12.5* Count words
  151. Sometimes you have to write a text with a maximum number of words. Vim can
  152. count the words for you.
  153. When the whole file is what you want to count the words in, use this
  154. command: >
  155. g CTRL-G
  156. Do not type a space after the g, this is just used here to make the command
  157. easy to read.
  158. The output looks like this:
  159. Col 1 of 0; Line 141 of 157; Word 748 of 774; Byte 4489 of 4976 ~
  160. You can see on which word you are (748), and the total number of words in the
  161. file (774).
  162. When the text is only part of a file, you could move to the start of the text,
  163. type "g CTRL-G", move to the end of the text, type "g CTRL-G" again, and then
  164. use your brain to compute the difference in the word position. That's a good
  165. exercise, but there is an easier way. With Visual mode, select the text you
  166. want to count words in. Then type g CTRL-G. The result:
  167. Selected 5 of 293 Lines; 70 of 1884 Words; 359 of 10928 Bytes ~
  168. For other ways to count words, lines and other items, see |count-items|.
  169. ==============================================================================
  170. *12.6* Find a man page *find-manpage*
  171. While editing a shell script or C program, you are using a command or function
  172. that you want to find the man page for (this is on Unix). Let's first use a
  173. simple way: Move the cursor to the word you want to find help on and press >
  174. K
  175. Vim will run the external "man" program on the word. If the man page is
  176. found, it is displayed. This uses the normal pager to scroll through the text
  177. (mostly the "more" program). When you get to the end pressing <Enter> will
  178. get you back into Vim.
  179. A disadvantage is that you can't see the man page and the text you are working
  180. on at the same time. There is a trick to make the man page appear in a Vim
  181. window. First, load the man filetype plugin: >
  182. :runtime! ftplugin/man.vim
  183. Put this command in your vimrc file if you intend to do this often. Now you
  184. can use the ":Man" command to open a window on a man page: >
  185. :Man csh
  186. You can scroll around and the text is highlighted. This allows you to find
  187. the help you were looking for. Use CTRL-W w to jump to the window with the
  188. text you were working on.
  189. To find a man page in a specific section, put the section number first.
  190. For example, to look in section 3 for "echo": >
  191. :Man 3 echo
  192. To jump to another man page, which is in the text with the typical form
  193. "word(1)", press CTRL-] on it. Further ":Man" commands will use the same
  194. window.
  195. To display a man page for the word under the cursor, use this: >
  196. \K
  197. (If you redefined the <Leader>, use it instead of the backslash).
  198. For example, you want to know the return value of "strstr()" while editing
  199. this line:
  200. if ( strstr (input, "aap") == ) ~
  201. Move the cursor to somewhere on "strstr" and type "\K". A window will open
  202. to display the man page for strstr().
  203. ==============================================================================
  204. *12.7* Trim blanks
  205. Some people find spaces and tabs at the end of a line useless, wasteful, and
  206. ugly. To remove whitespace at the end of every line, execute the following
  207. command: >
  208. :%s/\s\+$//
  209. The line range "%" is used, thus this works on the whole file. The pattern
  210. that the ":substitute" command matches with is "\s\+$". This finds white
  211. space characters (\s), 1 or more of them (\+), before the end-of-line ($).
  212. Later will be explained how you write patterns like this, see |usr_27.txt|.
  213. The "to" part of the substitute command is empty: "//". Thus it replaces
  214. with nothing, effectively deleting the matched white space.
  215. Another wasteful use of spaces is placing them before a tab. Often these can
  216. be deleted without changing the amount of white space. But not always!
  217. Therefore, you can best do this manually. Use this search command: >
  218. /
  219. You cannot see it, but there is a space before a tab in this command. Thus
  220. it's "/<Space><Tab>". Now use "x" to delete the space and check that the
  221. amount of white space doesn't change. You might have to insert a tab if it
  222. does change. Type "n" to find the next match. Repeat this until no more
  223. matches can be found.
  224. ==============================================================================
  225. *12.8* Find where a word is used
  226. If you are a UNIX user, you can use a combination of Vim and the grep command
  227. to edit all the files that contain a given word. This is extremely useful if
  228. you are working on a program and want to view or edit all the files that
  229. contain a specific variable.
  230. For example, suppose you want to edit all the C program files that contain
  231. the word "frame_counter". To do this you use the command: >
  232. vim `grep -l frame_counter *.c`
  233. Let's look at this command in detail. The grep command searches through a set
  234. of files for a given word. Because the -l argument is specified, the command
  235. will only list the files containing the word and not print the matching lines.
  236. The word it is searching for is "frame_counter". Actually, this can be any
  237. regular expression. (Note: What grep uses for regular expressions is not
  238. exactly the same as what Vim uses.)
  239. The entire command is enclosed in backticks (`). This tells the UNIX shell
  240. to run this command and pretend that the results were typed on the command
  241. line. So what happens is that the grep command is run and produces a list of
  242. files, these files are put on the Vim command line. This results in Vim
  243. editing the file list that is the output of grep. You can then use commands
  244. like ":next" and ":first" to browse through the files.
  245. FINDING EACH LINE
  246. The above command only finds the files in which the word is found. You still
  247. have to find the word within the files.
  248. Vim has a built-in command that you can use to search a set of files for a
  249. given string. If you want to find all occurrences of "error_string" in all C
  250. program files, for example, enter the following command: >
  251. :grep error_string *.c
  252. This causes Vim to search for the string "error_string" in all the specified
  253. files (*.c). The editor will now open the first file where a match is found
  254. and position the cursor on the first matching line. To go to the next
  255. matching line (no matter in what file it is), use the ":cnext" command. To go
  256. to the previous match, use the ":cprev" command. Use ":clist" to see all the
  257. matches and where they are.
  258. The ":grep" command uses the external commands grep (on Unix) or findstr
  259. (on Windows). You can change this by setting the option 'grepprg'.
  260. ==============================================================================
  261. Next chapter: |usr_20.txt| Typing command-line commands quickly
  262. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: