The use of Vim to browse the C/s code can be generated with ctags tag file, so it is convenient to jump to the function definition of the place, this function almost all the graphical interface editor, such as Vs,source insight and so on, but Vim's tags file is static, That is, if we add some new functions in the source code, the original tags are not automatically updated, we can not jump to the new function definition, how to do this problem?
I search a lot of places on the Internet, the general solution is to map the Ctags command to a shortcut key, so just click on the shortcut key will generate a new tags file, but there are a few inconvenient places
- Each tags file is a full-volume generation, if the project is very large, the creation of tags files may take more than 10 seconds, and the process of running the command is not able to edit the file, it is not possible to modify the file every time to update tags
- Vim run Ctags command is actually entered in the command mode
!ctags -R .
, it will be in the working directory of vim to generate tags files, and if your current working directory is not the directory you want to build tags, you have to switch directories
In short, want to automatically update tags, there is no such a simple thing!
But vs and source insight can do this, and we hold the principle that any other editor can implement vim to analyze the essence of the problem, in fact, whether it is vs or source insight they need to build engineering, and then import the source code into the project, We can assume that these editors will index the source files in the project, so that you can implement a variety of jump functions, when there is code updates or new source files, the editor can naturally detect, then it secretly re-indexed the source file, we can jump to the new function. Since the editor can do a lot of things secretly, why don't we do it, the computer's processor is free most of the time, not white
Imagine that we have a bunch of source code under Linux, we need to edit and read the code frequently, and its root directory structure should be like this
├── auto_tags├── build├── common├── include├── libs├── message├── metadata├── network├── nodes├── privacy├── tags└── tests
Where the tags file is the current directory of the Code production tag, the other directory is your source code, so when we open the directory source, vim can be based on the tags file to locate the location of the variable
Here the Auto_tags directory is the implementation of its own script, its function is to automatically detect the current directory of the source code is updated, if there is, the production of new tags file and replace the old, AUTO_TAGS directory structure as follows
├── auto_tags.conf├── auto_tags.sh└── run_tags.sh
Auto_tags.conf is a configuration file with the following configuration items
CTAGS=/usr/bin/ctags# 生成tags文件存放的目录tags_dir=../# 源代码所在目录source_root=../# 需要创建tags的目录名,注意只有目录名字,不是路径source_dirs="common message nodes"# 日志相关max_log_size=10 # Unit: Mblog_file="auto_tags.log"
Replace the Source_dirs variable with the directory name you need to build tags, note that you need to enclose it in double quotes, and write only the name of the directory, no need to add../
Use the following methods
sh ./run_tags.sh start : 启动脚本 sh ./run_tags.sh stop : 停止脚本
The core script is auto_tags.sh, as to how the script is implemented is not posted, after all, this is just a programmer to entertain the implementation of a small function, it is not perfect but easy to use. GitHub addresses are as follows
https://github.com/handy1989/vim/tree/master/auto_tags
The end ...
VIM updates tag automatically