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