usr_29.txt 20 KB

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