现在的位置: 首页 > 综合 > 正文

实现Vim tags自动更新

2013年06月26日 ⁄ 综合 ⁄ 共 1643字 ⁄ 字号 评论关闭

这里要用到两个插件:
projtags.vim : Set tags file for per project
AutoTag : Updates entries in a tags file automatically when saving

这两个插件单独工作都不能满足我的要求,但是他们合起来使用却是非常方便。
projtags.vim用于给项目配置tags文件,如配置项目路径为 /path-to-proj ,那么,当文件在 /path-to-proj及其子目录时,tags选项此时被局部地设置为了/path-to-proj/tags ,这样,即使是在子目录,tags选项的配置也能工作。
现在还有一个问题是tags自动更新的问题了。总不能每添加一个新文件就手动运行一下命令吧?而且,ctags的-a选项(append)并不会自动删除已经不存在的tag,此时,autotag.vim华丽登场~~,它能搞定这一切。

不过直接把这两个插件扔plugin目录是不会达到上述效果的,得稍做一些修改。
AutoTag.vim 作者的安装指导是要你直接在.vimrc里面source它。
这里我把它修改一下,然后放到~/.vim/plugin目录,给出补丁吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[admin@huangye plugin]$ diff -urNB ~/document/vim/plugins/autotag.vim autotag.vim

--- /home/admin/document/vim/plugins/autotag.vim    2012-01-06 16:50:37.912729822 +0800
+++ autotag.vim 2012-01-08 10:43:21.423301599 +0800
@@ -87,7 +87,7 @@
       while file:
          file = os.path.dirname(file)
          #self.__diag('drive = "%s", file = "%s"' %
(
drive, file))
-         tagsFile = os.path.join(drive, file, self.tags_file)
+         tagsFile = self.tags_file
          #self.__diag('tagsFile "%s"' % tagsFile)
          if os.path.isfile(tagsFile):
             st = os.stat(tagsFile)
@@ -184,7 +184,7 @@
    let g:autotagDisabled=0
 endif
 if !exists("g:autotagVerbosityLevel")
-   let g:autotagVerbosityLevel=0
+   let g:autotagVerbosityLevel=2
 endif
 if !exists("g:autotagExcludeSuffixes")
    let g:autotagExcludeSuffixes="tml.xml.text.txt"

projtags.vim的话,在

1
execute
'autocmd BufEnter '
.
filepattern .
' :setlocal tags+='
. tagfile

前面加上一句:

1
let g:autotagTagsFile=tagfile

,然后把其内容直接加到.vimrc里面即可。因为这里要让projtags.vim优先运行以告知autotag.vim tags文件的位置。
还有要注意的一点是,在给projtags.vim配置项目路径时,要写绝对路径,不能写 $HOME或 ~ ,不然autotag.vim不能更新tags.

1
:scriptnames

命令会列出你加载的插件列表,按加载顺序排列。不用总结哪个目录的插件先加载了,直接运行这条命令就好,一目了然。

====

file:///home/alan/Desktop/vim-tags-auto-update.html

【上篇】
【下篇】

抱歉!评论已关闭.