Ability
ctags可以使我们在函数调用和函数定义之间来回跳转,方便阅读源代。The ability to jump from the current source file to definitions of functions and structures in other files. A tag is an identifier that appears in a “tags” file. It is a sort of label that can be jumped to. For example: In C programs each function name can be used as a tag. The “tags” file has to be generated by a program like ctags, before the tag commands can be used.
原理
ctags分析源代码中的函数、变量、宏定义等信息,将这些tag的名字和它们所在的文件,以及如何通过命令跳转到这些tag等信息保存到名为tags的文件中。
用法
ctags -R *
要想使用ctags带来的在函数调用和函数定义之间来回跳转的功能,首先要生成名为tags的文件。在shell环境中切换到源代码根目录,输入命令ctags -R *
,即可在源代码根目录下生成tags文件。vim -t <tag>
To search for a specific tag and open Vim to its definition, run the above command in your shell.Open any source file in Vim and use the following basic commands:
Keyboard command | Action |
---|---|
Ctrl-] | Jump to the tag underneath the cursor |
Ctrl-t | Jump back up in the tag stack |
:tag <tag> |
Position the cursor on the tag |
:ts <tag> |
Search for a particular tag |
:tags | Show where you are in the tag stack |
:tn | Go to the next definition for the last tag |
:tp | Go to the previous definition for the last tag |
注意
运行vim的时候,必须在tags文件所在的目录下运行,因为vim启动时会在当前目录内寻找tags文件并加载它。否则,运行vim的时候还要用:set tags=
命令设定tags文件的路径,这样vim才能找到tags文件。