robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

master
19.6 KiB614 linesraw
1*usr_29.txt*	Nvim
2
3
4		     VIM USER MANUAL	by Bram Moolenaar
5
6
7			    Moving through programs
8
9
10The creator of Vim is a computer programmer.  It's no surprise that Vim
11contains many features to aid in writing programs.  Jump around to find where
12identifiers are defined and used.  Preview declarations in a separate window.
13There 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
23Table of contents: |usr_toc.txt|
24
25==============================================================================
26*29.1*	Using tags
27
28What is a tag?  It is a location where an identifier is defined.  An example
29is a function definition in a C or C++ program.  A list of tags is kept in a
30tags file.  This can be used by Vim to directly jump from any place to the
31tag, the place where an identifier is defined.
32   To generate the tags file for all C files in the current directory, use the
33following command: >
34
35	ctags *.c
36
37"ctags" is a separate program.  Most Unix systems already have it installed.
38If you do not have it yet, you can find Universal ctags at:
39	https://ctags.io
40
41Universal ctags is preferred, Exuberant ctags is no longer being developed.
42
43Now when you are in Vim and you want to go to a function definition, you can
44jump to it by using the following command: >
45
46	:tag startlist
47
48This 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.
50This makes it easy to explore a tangle of C code.  Suppose, for example, that
51you 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
53call to "write_line" and pressing CTRL-], you jump to the definition of this
54function.
55   The "write_line" function calls "write_char".  You need to figure out what
56it does.  So you position the cursor over the call to "write_char" and press
57CTRL-].  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<
85The ":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<
93Now to go back.  The CTRL-T command goes to the preceding tag.  In the example
94above 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
96back.  You have gone forward, and now back.  Let's go forward again.  The
97following command goes to the tag on top of the list: >
98
99	:tag
100
101You 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
104up again with CTRL-T.  Use ":tags" to find out where you are.
105
106
107SPLIT WINDOWS
108
109The ":tag" command replaces the file in the current window with the one
110containing the new function.  But suppose you want to see not only the old
111function but also the new one?  You can split the window using the ":split"
112command followed by the ":tag" command.  Vim has a shorthand command that does
113both: >
114	:stag tagname
115
116To split the current window and jump to the tag under the cursor use this
117command: >
118
119	CTRL-W ]
120
121If a count is specified, the new window will be that many lines high.
122
123
124MORE TAGS FILES
125
126When you have files in many directories, you can create a tags file in each of
127them.  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
129tags files.  Example: >
130
131	:set tags=./tags,./../tags,./*/tags
132
133This finds a tags file in the same directory as the current file, one
134directory level higher and in all subdirectories.
135   This is quite a number of tags files, but it may still not be enough.  For
136example, 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
138tree for tags files.  Example: >
139
140	:set tags=~/proj/**/tags
141
142
143ONE TAGS FILE
144
145When Vim has to search many places for tags files, you can hear the disk
146rattling.  It may get a bit slow.  In that case it's better to spend this
147time while generating one big tags file.  You might do this overnight.
148   This requires the Universal or Exuberant ctags program, mentioned above.
149It offers an argument to search a whole directory tree: >
150
151	cd ~/proj
152	ctags -R .
153
154The nice thing about this is that Universal/Exuberant ctags recognizes various
155file types.  Thus this doesn't work just for C and C++ programs, also for
156Eiffel 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
162MULTIPLE MATCHES
163
164When a function is defined multiple times (or a method in several classes),
165the ":tag" command will jump to the first one.  If there is a match in the
166current file, that one is used first.
167   You can now jump to other matches for the same tag with: >
168
169	:tnext
170
171Repeat this to find further matches.  If there are many, you can select which
172one to jump to: >
173
174	:tselect tagname
175
176Vim 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
189You can now enter the number (in the first column) of the match that you would
190like to jump to.  The information in the other columns give you a good idea of
191where the match is defined.
192
193To 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
200If [count] is omitted then one is used.
201
202
203GUESSING TAG NAMES
204
205Command line completion is a good way to avoid typing a long tag name.  Just
206type the first bit and press <Tab>: >
207
208	:tag write_<Tab>
209
210You will get the first match.  If it's not the one you want, press <Tab> until
211you find the right one.
212   Sometimes you only know part of the name of a function.  Or you have many
213tags that start with the same string, but end differently.  Then you can tell
214Vim to use a pattern to find the tag.
215   Suppose you want to jump to a tag that contains "block".  First type
216this: >
217
218	:tag /block
219
220Now use command line completion: press <Tab>.  Vim will find all tags that
221contain "block" and use the first match.
222   The "/" before a tag name tells Vim that what follows is not a literal tag
223name, but a pattern.  You can use all the items for search patterns here.  For
224example, suppose you want to select a tag that starts with "write_": >
225
226	:tselect /^write_
227
228The "^" specifies that the tag starts with "write_".  Otherwise it would also
229be found halfway in a tag name.  Similarly "$" at the end makes sure the
230pattern matches until the end of a tag.
231
232
233A TAGS BROWSER
234
235Since CTRL-] takes you to the definition of the identifier under the cursor,
236you can use a list of identifier names as a table of contents.  Here is an
237example.
238   First create a list of identifiers (this requires Universal or Exuberant
239ctags): >
240
241	ctags --c-types=f -f functions *.c
242
243Now start Vim without a file, and edit this file in Vim, in a vertically split
244window: >
245
246	vim
247	:vsplit functions
248
249The window contains a list of all the functions.  There is some more stuff,
250but 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
255Move the cursor to the line that contains the function you want to go to.
256Now press <Enter>.  Vim will go to the other window and jump to the selected
257function.
258
259
260RELATED ITEMS
261
262To make case in tag names be ignored, you can set 'ignorecase' while leaving
263'tagcase' as "followic", or set 'tagcase' to "ignore".
264
265The 'tagbsearch' option tells if the tags file is sorted or not.  The default
266is to assume a sorted tags file, which makes a tags search a lot faster, but
267doesn't work if the tags file isn't sorted.
268
269The 'taglength' option can be used to tell Vim the number of significant
270characters in a tag.
271
272==============================================================================
273*29.2*	The preview window
274
275When you edit code that contains a function call, you need to use the correct
276arguments.  To know what values to pass you can look at how the function is
277defined.  The tags mechanism works very well for this.  Preferably the
278definition is displayed in another window.  For this the preview window can be
279used.
280   To open a preview window to display the function "write_char": >
281
282	:ptag write_char
283
284Vim will open a window, and jumps to the tag "write_char".  Then it takes you
285back to the original position.  Thus you can continue typing without the need
286to use a CTRL-W command.
287   If the name of a function appears in the text, you can get its definition
288in the preview window with: >
289
290	CTRL-W }
291
292There is a script that automatically displays the text where the word under
293the cursor was defined.  See |CursorHold-example|.
294
295To close the preview window use this command: >
296
297	:pclose
298
299To edit a specific file in the preview window, use ":pedit".  This can be
300useful to edit a header file, for example: >
301
302	:pedit defs.h
303
304Finally, ":psearch" can be used to find a word in the current file and any
305included files and display the match in the preview window.  This is
306especially useful when using library functions, for which you do not have a
307tags file.  Example: >
308
309	:psearch popen
310
311This will show the "stdio.h" file in the preview window, with the function
312prototype for popen(): >c
313
314	FILE	*popen __P((const char *, const char *));
315
316You 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
322Since a program is structured, Vim can recognize items in it.  Specific
323commands 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
332But then much longer, and possibly nested.  Position the cursor on the
333"#ifdef" and press %.  Vim will jump to the "#else".  Pressing % again takes
334you 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
336good 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
338of it with: >
339
340	[#
341
342If you are not after a "#if" or "#ifdef" Vim will beep.  To jump forward to
343the next "#else" or "#endif" use: >
344
345	]#
346
347These two commands skip any "#if" - "#endif" blocks that they encounter.
348Example:
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
357With the cursor in the last line, "[#" moves to the first line.  The "#ifdef"
358- "#endif" block in the middle is skipped.
359
360
361MOVING IN CODE BLOCKS
362
363In C code blocks are enclosed in {}.  These can get pretty long.  To move to
364the start of the outer block use the "[[" command.  Use "][" to find the end.
365This assumes that the "{" and "}" are in the first column.
366   The [{ command moves to the start of the current block.  It skips over
367pairs 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
384When writing C++ or Java, the outer {} block is for the class.  The next level
385of {} is for a method.  When somewhere inside a class use "[m" to find the
386previous start of a method.  "]m" finds the next start of a method.
387
388Additionally, "[]" moves backward to the end of a function and "]]" moves
389forward to the start of the next function.  The end of a function is defined
390by 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
409Don't forget you can also use "%" to move between matching (), {} and [].
410That also works when they are many lines apart.
411
412
413MOVING IN BRACES
414
415The [( and ]) commands work similar to [{ and ]}, except that they
416work on () pairs instead of {} pairs.
417>
418				  [(
419<		    <--------------------------------
420			      <-------
421		if (a == b && (c == d || (e > f)) && x > y) ~
422				  -------------->
423			  --------------------------------> >
424				       ])
425
426MOVING IN COMMENTS
427
428To move back to the start of a comment use "[/".  Move forward to the end of a
429comment 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
443You 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
449Vim will list the matching lines it can find.  Not only in the current file,
450but also in all included files (and files included in them, etc.).  The result
451looks like this: >
452
453	structs.h
454	 1:   29     unsigned     column;    /* column number */
455
456The advantage over using tags or the preview window is that included files are
457searched.  In most cases this results in the right declaration to be found.
458Also when the tags file is out of date.  Also when you don't have tags for the
459included files.
460   However, a few things must be right for "[I" to do its work.  First of all,
461the 'include' option must specify how a file is included.  The default value
462works for C and C++.  For other languages you will have to change it.
463
464
465LOCATING INCLUDED FILES
466
467   Vim will find included files in the places specified with the 'path'
468option.  If a directory is missing, some include files will not be found.  You
469can discover this with this command: >
470
471	:checkpath
472
473It will list the include files that could not be found.  Also files included
474by 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
482The "io.h" file is included by the current file and can't be found.  "vim.h"
483can be found, thus ":checkpath" goes into this file and checks what it
484includes.  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
492To fix the files that could not be found, add a directory to the 'path'
493option.  A good place to find out about this is the Makefile.  Look out for
494lines that contain "-I" items, like "-I/usr/local/X11".  To add this directory
495use: >
496
497	:set path+=/usr/local/X11
498
499When there are many subdirectories, you can use the "*" wildcard.  Example: >
500
501	:set path+=/usr/*/include
502
503This would find files in "/usr/local/include" as well as "/usr/X11/include".
504
505When working on a project with a whole nested tree of included files, the "**"
506items is useful.  This will search down in all subdirectories.  Example: >
507
508	:set path+=/projects/invent/**/include
509
510This 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
517There 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
519command: >
520
521	:checkpath!
522
523You will get a (very long) list of included files, the files they include, and
524so on.  To shorten the list a bit, Vim shows "(Already listed)" for files that
525were found before and doesn't list the included files in there again.
526
527
528JUMPING TO A MATCH
529
530"[I" produces a list with only one line of text.  When you want to have a
531closer look at the first item, you can jump to that line with the command: >
532
533	[<Tab>
534
535You can also use "[ CTRL-I", since CTRL-I is the same as pressing <Tab>.
536
537The list that "[I" produces has a number at the start of each line.  When you
538want to jump to another item than the first one, type the number first: >
539
540	3[<Tab>
541
542Will jump to the third item in the list.  Remember that you can use CTRL-O to
543jump back to where you started from.
544
545
546RELATED 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
553FINDING DEFINED IDENTIFIERS
554
555The "[I" command finds any identifier.  To find only macros, defined with
556"#define" use: >
557
558	[D
559
560Again, this searches in included files.  The 'define' option specifies what a
561line looks like that defines the items for "[D".  You could change it to make
562it 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
572The "[I" command searches included files.  To search in the current file only,
573and jump to the first place where the word under the cursor is used: >
574
575	gD
576
577Hint: Goto Definition.  This command is very useful to find a variable or
578function 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<
589To restrict the search even further, and look only in the current function,
590use this command: >
591
592	gd
593
594This will go back to the start of the current function and find the first
595occurrence of the word under the cursor.  Actually, it searches backwards to
596an empty line above a "{" in the first column.  From there it searches forward
597for 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
610Next chapter: |usr_30.txt|  Editing programs
611
612Copyright: see |manual-copyright|
613
614vim:tw=78:ts=8:noet:ft=help:norl: