usr_29.txt 20 KB

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