*usr_29.txt* Nvim
VIM USER MANUAL - by Bram Moolenaar
Moving through programs
The creator of Vim is a computer programmer. It's no surprise that Vim
contains many features to aid in writing programs. Jump around to find where
identifiers are defined and used. Preview declarations in a separate window.
There is more in the next chapter.
|29.1| Using tags
|29.2| The preview window
|29.3| Moving through a program
|29.4| Finding global identifiers
|29.5| Finding local identifiers
Next chapter: |usr_30.txt| Editing programs
Previous chapter: |usr_28.txt| Folding
Table of contents: |usr_toc.txt|
==============================================================================
*29.1* Using tags
What is a tag? It is a location where an identifier is defined. An example
is a function definition in a C or C++ program. A list of tags is kept in a
tags file. This can be used by Vim to directly jump from any place to the
tag, the place where an identifier is defined.
To generate the tags file for all C files in the current directory, use the
following command: >
ctags *.c
"ctags" is a separate program. Most Unix systems already have it installed.
If you do not have it yet, you can find Universal ctags at:
https://ctags.io
Universal ctags is preferred, Exuberant ctags is no longer being developed.
Now when you are in Vim and you want to go to a function definition, you can
jump to it by using the following command: >
:tag startlist
This command will find the function "startlist" even if it is in another file.
The CTRL-] command jumps to the tag of the word that is under the cursor.
This makes it easy to explore a tangle of C code. Suppose, for example, that
you are in the function "write_block". You can see that it calls
"write_line". But what does "write_line" do? By placing the cursor on the
call to "write_line" and pressing CTRL-], you jump to the definition of this
function.
The "write_line" function calls "write_char". You need to figure out what
it does. So you position the cursor over the call to "write_char" and press
CTRL-]. Now you are at the definition of "write_char".
>
+-------------------------------------+
|void write_block(char **s; int cnt) |
|{ |
| int i; |
| for (i = 0; i < cnt; ++i) |
| write_line(s[i]); |
|} | |
+-----------|-------------------------+
|
CTRL-] |
| +----------------------------+
+--> |void write_line(char *s) |
|{ |
| while (*s != 0) |
| write_char(*s++); |
|} | |
+--------|-------------------+
|
CTRL-] |
| +------------------------------------+
+--> |void write_char(char c) |
|{ |
| putchar((int)(unsigned char)c); |
|} |
+------------------------------------+
<
The ":tags" command shows the list of tags that you traversed through:
:tags
# TO tag FROM line in file/text ~
1 1 write_line 8 write_block.c ~
2 1 write_char 7 write_line.c ~
> ~
<
Now to go back. The CTRL-T command goes to the preceding tag. In the example
above you get back to the "write_line" function, in the call to "write_char".
This command takes a count argument that indicates how many tags to jump
back. You have gone forward, and now back. Let's go forward again. The
following command goes to the tag on top of the list: >
:tag
You can prefix it with a count and jump forward that many tags. For example:
":3tag". CTRL-T also can be preceded with a count.
These commands thus allow you to go down a call tree with CTRL-] and back
up again with CTRL-T. Use ":tags" to find out where you are.
SPLIT WINDOWS
The ":tag" command replaces the file in the current window with the one
containing the new function. But suppose you want to see not only the old
function but also the new one? You can split the window using the ":split"