usr_29.txt (20046B)
1 *usr_29.txt* Nvim 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 Moving through programs 8 9 10 The creator of Vim is a computer programmer. It's no surprise that Vim 11 contains many features to aid in writing programs. Jump around to find where 12 identifiers are defined and used. Preview declarations in a separate window. 13 There is more in the next chapter. 14 15 |29.1| Using tags 16 |29.2| The preview window 17 |29.3| Moving through a program 18 |29.4| Finding global identifiers 19 |29.5| Finding local identifiers 20 21 Next chapter: |usr_30.txt| Editing programs 22 Previous chapter: |usr_28.txt| Folding 23 Table of contents: |usr_toc.txt| 24 25 ============================================================================== 26 *29.1* Using tags 27 28 What is a tag? It is a location where an identifier is defined. An example 29 is a function definition in a C or C++ program. A list of tags is kept in a 30 tags file. This can be used by Vim to directly jump from any place to the 31 tag, the place where an identifier is defined. 32 To generate the tags file for all C files in the current directory, use the 33 following command: > 34 35 ctags *.c 36 37 "ctags" is a separate program. Most Unix systems already have it installed. 38 If you do not have it yet, you can find Universal ctags at: 39 https://ctags.io 40 41 Universal ctags is preferred, Exuberant ctags is no longer being developed. 42 43 Now when you are in Vim and you want to go to a function definition, you can 44 jump to it by using the following command: > 45 46 :tag startlist 47 48 This command will find the function "startlist" even if it is in another file. 49 The CTRL-] command jumps to the tag of the word that is under the cursor. 50 This makes it easy to explore a tangle of C code. Suppose, for example, that 51 you are in the function "write_block". You can see that it calls 52 "write_line". But what does "write_line" do? By placing the cursor on the 53 call to "write_line" and pressing CTRL-], you jump to the definition of this 54 function. 55 The "write_line" function calls "write_char". You need to figure out what 56 it does. So you position the cursor over the call to "write_char" and press 57 CTRL-]. Now you are at the definition of "write_char". 58 > 59 +-------------------------------------+ 60 |void write_block(char **s; int cnt) | 61 |{ | 62 | int i; | 63 | for (i = 0; i < cnt; ++i) | 64 | write_line(s[i]); | 65 |} | | 66 +-----------|-------------------------+ 67 | 68 CTRL-] | 69 | +----------------------------+ 70 +--> |void write_line(char *s) | 71 |{ | 72 | while (*s != 0) | 73 | write_char(*s++); | 74 |} | | 75 +--------|-------------------+ 76 | 77 CTRL-] | 78 | +------------------------------------+ 79 +--> |void write_char(char c) | 80 |{ | 81 | putchar((int)(unsigned char)c); | 82 |} | 83 +------------------------------------+ 84 < 85 The ":tags" command shows the list of tags that you traversed through: 86 87 :tags 88 # TO tag FROM line in file/text ~ 89 1 1 write_line 8 write_block.c ~ 90 2 1 write_char 7 write_line.c ~ 91 > ~ 92 < 93 Now to go back. The CTRL-T command goes to the preceding tag. In the example 94 above you get back to the "write_line" function, in the call to "write_char". 95 This command takes a count argument that indicates how many tags to jump 96 back. You have gone forward, and now back. Let's go forward again. The 97 following command goes to the tag on top of the list: > 98 99 :tag 100 101 You can prefix it with a count and jump forward that many tags. For example: 102 ":3tag". CTRL-T also can be preceded with a count. 103 These commands thus allow you to go down a call tree with CTRL-] and back 104 up again with CTRL-T. Use ":tags" to find out where you are. 105 106 107 SPLIT WINDOWS 108 109 The ":tag" command replaces the file in the current window with the one 110 containing the new function. But suppose you want to see not only the old 111 function but also the new one? You can split the window using the ":split" 112 command followed by the ":tag" command. Vim has a shorthand command that does 113 both: > 114 :stag tagname 115 116 To split the current window and jump to the tag under the cursor use this 117 command: > 118 119 CTRL-W ] 120 121 If a count is specified, the new window will be that many lines high. 122 123 124 MORE TAGS FILES 125 126 When you have files in many directories, you can create a tags file in each of 127 them. Vim will then only be able to jump to tags within that directory. 128 To find more tags files, set the 'tags' option to include all the relevant 129 tags files. Example: > 130 131 :set tags=./tags,./../tags,./*/tags 132 133 This finds a tags file in the same directory as the current file, one 134 directory level higher and in all subdirectories. 135 This is quite a number of tags files, but it may still not be enough. For 136 example, when editing a file in "~/proj/src", you will not find the tags file 137 "~/proj/sub/tags". For this situation Vim offers to search a whole directory 138 tree for tags files. Example: > 139 140 :set tags=~/proj/**/tags 141 142 143 ONE TAGS FILE 144 145 When Vim has to search many places for tags files, you can hear the disk 146 rattling. It may get a bit slow. In that case it's better to spend this 147 time while generating one big tags file. You might do this overnight. 148 This requires the Universal or Exuberant ctags program, mentioned above. 149 It offers an argument to search a whole directory tree: > 150 151 cd ~/proj 152 ctags -R . 153 154 The nice thing about this is that Universal/Exuberant ctags recognizes various 155 file types. Thus this doesn't work just for C and C++ programs, also for 156 Eiffel and even Vim scripts. See the ctags documentation to tune this. 157 Now you only need to tell Vim where your big tags file is: > 158 159 :set tags=~/proj/tags 160 161 162 MULTIPLE MATCHES 163 164 When a function is defined multiple times (or a method in several classes), 165 the ":tag" command will jump to the first one. If there is a match in the 166 current file, that one is used first. 167 You can now jump to other matches for the same tag with: > 168 169 :tnext 170 171 Repeat this to find further matches. If there are many, you can select which 172 one to jump to: > 173 174 :tselect tagname 175 176 Vim will present you with a list of choices: 177 178 # pri kind tag file ~ 179 1 F f mch_init os_amiga.c ~ 180 mch_init() ~ 181 2 F f mch_init os_mac.c ~ 182 mch_init() ~ 183 3 F f mch_init os_msdos.c ~ 184 mch_init(void) ~ 185 4 F f mch_init os_riscos.c ~ 186 mch_init() ~ 187 Enter nr of choice (<CR> to abort): ~ 188 189 You can now enter the number (in the first column) of the match that you would 190 like to jump to. The information in the other columns give you a good idea of 191 where the match is defined. 192 193 To move between the matching tags, these commands can be used: 194 195 :tfirst go to first match 196 :[count]tprevious go to [count] previous match 197 :[count]tnext go to [count] next match 198 :tlast go to last match 199 200 If [count] is omitted then one is used. 201 202 203 GUESSING TAG NAMES 204 205 Command line completion is a good way to avoid typing a long tag name. Just 206 type the first bit and press <Tab>: > 207 208 :tag write_<Tab> 209 210 You will get the first match. If it's not the one you want, press <Tab> until 211 you find the right one. 212 Sometimes you only know part of the name of a function. Or you have many 213 tags that start with the same string, but end differently. Then you can tell 214 Vim to use a pattern to find the tag. 215 Suppose you want to jump to a tag that contains "block". First type 216 this: > 217 218 :tag /block 219 220 Now use command line completion: press <Tab>. Vim will find all tags that 221 contain "block" and use the first match. 222 The "/" before a tag name tells Vim that what follows is not a literal tag 223 name, but a pattern. You can use all the items for search patterns here. For 224 example, suppose you want to select a tag that starts with "write_": > 225 226 :tselect /^write_ 227 228 The "^" specifies that the tag starts with "write_". Otherwise it would also 229 be found halfway in a tag name. Similarly "$" at the end makes sure the 230 pattern matches until the end of a tag. 231 232 233 A TAGS BROWSER 234 235 Since CTRL-] takes you to the definition of the identifier under the cursor, 236 you can use a list of identifier names as a table of contents. Here is an 237 example. 238 First create a list of identifiers (this requires Universal or Exuberant 239 ctags): > 240 241 ctags --c-types=f -f functions *.c 242 243 Now start Vim without a file, and edit this file in Vim, in a vertically split 244 window: > 245 246 vim 247 :vsplit functions 248 249 The window contains a list of all the functions. There is some more stuff, 250 but you can ignore that. Do ":setlocal ts=99" to clean it up a bit. 251 In this window, define a mapping: > 252 253 :nnoremap <buffer> <CR> 0ye<C-W>w:tag <C-R>"<CR> 254 255 Move the cursor to the line that contains the function you want to go to. 256 Now press <Enter>. Vim will go to the other window and jump to the selected 257 function. 258 259 260 RELATED ITEMS 261 262 To make case in tag names be ignored, you can set 'ignorecase' while leaving 263 'tagcase' as "followic", or set 'tagcase' to "ignore". 264 265 The 'tagbsearch' option tells if the tags file is sorted or not. The default 266 is to assume a sorted tags file, which makes a tags search a lot faster, but 267 doesn't work if the tags file isn't sorted. 268 269 The 'taglength' option can be used to tell Vim the number of significant 270 characters in a tag. 271 272 ============================================================================== 273 *29.2* The preview window 274 275 When you edit code that contains a function call, you need to use the correct 276 arguments. To know what values to pass you can look at how the function is 277 defined. The tags mechanism works very well for this. Preferably the 278 definition is displayed in another window. For this the preview window can be 279 used. 280 To open a preview window to display the function "write_char": > 281 282 :ptag write_char 283 284 Vim will open a window, and jumps to the tag "write_char". Then it takes you 285 back to the original position. Thus you can continue typing without the need 286 to use a CTRL-W command. 287 If the name of a function appears in the text, you can get its definition 288 in the preview window with: > 289 290 CTRL-W } 291 292 There is a script that automatically displays the text where the word under 293 the cursor was defined. See |CursorHold-example|. 294 295 To close the preview window use this command: > 296 297 :pclose 298 299 To edit a specific file in the preview window, use ":pedit". This can be 300 useful to edit a header file, for example: > 301 302 :pedit defs.h 303 304 Finally, ":psearch" can be used to find a word in the current file and any 305 included files and display the match in the preview window. This is 306 especially useful when using library functions, for which you do not have a 307 tags file. Example: > 308 309 :psearch popen 310 311 This will show the "stdio.h" file in the preview window, with the function 312 prototype for popen(): >c 313 314 FILE *popen __P((const char *, const char *)); 315 316 You can specify the height of the preview window, when it is opened, with the 317 'previewheight' option. 318 319 ============================================================================== 320 *29.3* Moving through a program 321 322 Since a program is structured, Vim can recognize items in it. Specific 323 commands can be used to move around. 324 C programs often contain constructs like this: >c 325 326 #ifdef USE_POPEN 327 fd = popen("ls", "r") 328 #else 329 fd = fopen("tmp", "w") 330 #endif 331 332 But then much longer, and possibly nested. Position the cursor on the 333 "#ifdef" and press %. Vim will jump to the "#else". Pressing % again takes 334 you to the "#endif". Another % takes you to the "#ifdef" again. 335 When the construct is nested, Vim will find the matching items. This is a 336 good way to check if you didn't forget an "#endif". 337 When you are somewhere inside a "#if" - "#endif", you can jump to the start 338 of it with: > 339 340 [# 341 342 If you are not after a "#if" or "#ifdef" Vim will beep. To jump forward to 343 the next "#else" or "#endif" use: > 344 345 ]# 346 347 These two commands skip any "#if" - "#endif" blocks that they encounter. 348 Example: 349 350 #if defined(HAS_INC_H) ~ 351 a = a + inc(); ~ 352 # ifdef USE_THEME ~ 353 a += 3; ~ 354 # endif ~ 355 set_width(a); ~ 356 357 With the cursor in the last line, "[#" moves to the first line. The "#ifdef" 358 - "#endif" block in the middle is skipped. 359 360 361 MOVING IN CODE BLOCKS 362 363 In C code blocks are enclosed in {}. These can get pretty long. To move to 364 the start of the outer block use the "[[" command. Use "][" to find the end. 365 This assumes that the "{" and "}" are in the first column. 366 The [{ command moves to the start of the current block. It skips over 367 pairs of {} at the same level. "]}" jumps to the end. 368 An overview: 369 370 function(int a) 371 +-> { 372 | if (a) 373 | +-> { 374 [[ | | for (;;) --+ 375 | | +-> { | 376 | [{ | | foo(32); | --+ 377 | | [{ | if (bar(a)) --+ | ]} | 378 +-- | +-- break; | ]} | | 379 | } <-+ | | ][ 380 +-- foobar(a) | | 381 } <-+ | 382 } <-+ 383 384 When writing C++ or Java, the outer {} block is for the class. The next level 385 of {} is for a method. When somewhere inside a class use "[m" to find the 386 previous start of a method. "]m" finds the next start of a method. 387 388 Additionally, "[]" moves backward to the end of a function and "]]" moves 389 forward to the start of the next function. The end of a function is defined 390 by a "}" in the first column. 391 392 int func1(void) 393 { 394 return 1; 395 +----------> } 396 | 397 [] | int func2(void) 398 | +-> { 399 | [[ | if (flag) 400 start +-- +-- return flag; 401 | ][ | return 2; 402 | +-> } 403 ]] | 404 | int func3(void) 405 +----------> { 406 return 3; 407 } 408 409 Don't forget you can also use "%" to move between matching (), {} and []. 410 That also works when they are many lines apart. 411 412 413 MOVING IN BRACES 414 415 The [( and ]) commands work similar to [{ and ]}, except that they 416 work on () pairs instead of {} pairs. 417 > 418 [( 419 < <-------------------------------- 420 <------- 421 if (a == b && (c == d || (e > f)) && x > y) ~ 422 --------------> 423 --------------------------------> > 424 ]) 425 426 MOVING IN COMMENTS 427 428 To move back to the start of a comment use "[/". Move forward to the end of a 429 comment with "]/". This only works for `/* - */` comments. 430 > 431 +-> +-> /* 432 | [/ | * A comment about --+ 433 [/ | +-- * wonderful life. | ]/ 434 | */ <-+ 435 | 436 +-- foo = bar * 3; --+ 437 | ]/ 438 /* a short comment */ <-+ 439 < 440 ============================================================================== 441 *29.4* Finding global identifiers 442 443 You are editing a C program and wonder if a variable is declared as "int" or 444 "unsigned". A quick way to find this is with the "[I" command. 445 Suppose the cursor is on the word "column". Type: > 446 447 [I 448 449 Vim will list the matching lines it can find. Not only in the current file, 450 but also in all included files (and files included in them, etc.). The result 451 looks like this: > 452 453 structs.h 454 1: 29 unsigned column; /* column number */ 455 456 The advantage over using tags or the preview window is that included files are 457 searched. In most cases this results in the right declaration to be found. 458 Also when the tags file is out of date. Also when you don't have tags for the 459 included files. 460 However, a few things must be right for "[I" to do its work. First of all, 461 the 'include' option must specify how a file is included. The default value 462 works for C and C++. For other languages you will have to change it. 463 464 465 LOCATING INCLUDED FILES 466 467 Vim will find included files in the places specified with the 'path' 468 option. If a directory is missing, some include files will not be found. You 469 can discover this with this command: > 470 471 :checkpath 472 473 It will list the include files that could not be found. Also files included 474 by the files that could be found. An example of the output: 475 476 --- Included files not found in path --- ~ 477 <io.h> ~ 478 vim.h --> ~ 479 <functions.h> ~ 480 <clib/exec_protos.h> ~ 481 482 The "io.h" file is included by the current file and can't be found. "vim.h" 483 can be found, thus ":checkpath" goes into this file and checks what it 484 includes. The "functions.h" and "clib/exec_protos.h" files, included by 485 "vim.h" are not found. 486 487 Note: 488 Vim is not a compiler. It does not recognize "#ifdef" statements. 489 This means every "#include" statement is used, also when it comes 490 after "#if NEVER". 491 492 To fix the files that could not be found, add a directory to the 'path' 493 option. A good place to find out about this is the Makefile. Look out for 494 lines that contain "-I" items, like "-I/usr/local/X11". To add this directory 495 use: > 496 497 :set path+=/usr/local/X11 498 499 When there are many subdirectories, you can use the "*" wildcard. Example: > 500 501 :set path+=/usr/*/include 502 503 This would find files in "/usr/local/include" as well as "/usr/X11/include". 504 505 When working on a project with a whole nested tree of included files, the "**" 506 items is useful. This will search down in all subdirectories. Example: > 507 508 :set path+=/projects/invent/**/include 509 510 This will find files in the directories: 511 512 /projects/invent/include ~ 513 /projects/invent/main/include ~ 514 /projects/invent/main/os/include ~ 515 etc. 516 517 There are even more possibilities. Check out the 'path' option for info. 518 If you want to see which included files are actually found, use this 519 command: > 520 521 :checkpath! 522 523 You will get a (very long) list of included files, the files they include, and 524 so on. To shorten the list a bit, Vim shows "(Already listed)" for files that 525 were found before and doesn't list the included files in there again. 526 527 528 JUMPING TO A MATCH 529 530 "[I" produces a list with only one line of text. When you want to have a 531 closer look at the first item, you can jump to that line with the command: > 532 533 [<Tab> 534 535 You can also use "[ CTRL-I", since CTRL-I is the same as pressing <Tab>. 536 537 The list that "[I" produces has a number at the start of each line. When you 538 want to jump to another item than the first one, type the number first: > 539 540 3[<Tab> 541 542 Will jump to the third item in the list. Remember that you can use CTRL-O to 543 jump back to where you started from. 544 545 546 RELATED COMMANDS 547 548 [i only lists the first match 549 ]I only lists items below the cursor 550 ]i only lists the first item below the cursor 551 552 553 FINDING DEFINED IDENTIFIERS 554 555 The "[I" command finds any identifier. To find only macros, defined with 556 "#define" use: > 557 558 [D 559 560 Again, this searches in included files. The 'define' option specifies what a 561 line looks like that defines the items for "[D". You could change it to make 562 it work with other languages than C or C++. 563 The commands related to "[D" are: 564 565 [d only lists the first match 566 ]D only lists items below the cursor 567 ]d only lists the first item below the cursor 568 569 ============================================================================== 570 *29.5* Finding local identifiers 571 572 The "[I" command searches included files. To search in the current file only, 573 and jump to the first place where the word under the cursor is used: > 574 575 gD 576 577 Hint: Goto Definition. This command is very useful to find a variable or 578 function that was declared locally ("static", in C terms). Example (cursor on 579 "counter"): 580 > 581 +-> static int counter = 0; 582 | 583 | int get_counter(void) 584 gD | { 585 | ++counter; 586 +-- return counter; 587 } 588 < 589 To restrict the search even further, and look only in the current function, 590 use this command: > 591 592 gd 593 594 This will go back to the start of the current function and find the first 595 occurrence of the word under the cursor. Actually, it searches backwards to 596 an empty line above a "{" in the first column. From there it searches forward 597 for the identifier. Example (cursor on "idx"): 598 > 599 int find_entry(char *name) 600 { 601 +-> int idx; 602 | 603 gd | for (idx = 0; idx < table_len; ++idx) 604 | if (strcmp(table[idx].name, name) == 0) 605 +-- return idx; 606 } 607 < 608 ============================================================================== 609 610 Next chapter: |usr_30.txt| Editing programs 611 612 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: