Vim项目tags文件问题 » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

Vim项目tags文件问题

貌似国人有个插件:
projtags.vim

用的时候这样:

let g:ProjTags = [ "~/work/proj1" ] 

要用这个插件,可修改.vimrc文件或者新建一.vim文件再在.vimrc文件里面source之。
直接把它的说明copy来吧:

Usage:

Set your projects path into g:ProjTags as a list, for example:

let g:ProjTags = [ "~/work/proj1" ]

In this case for all file match the pattern “~/work/proj1/*”, the tag file
“~/work/proj1/tags” will be used.

You can also specify a tags file other than the default one or more tags
files for one directory by a list:

let g:ProjTags += [[ "~/work/proj2", "~/work/proj2/tags",
"~/work/common.tags" ]]

In this case for all files match the pattern “~/work/proj2/*”, the tag files
“~/work/proj2/tags” and “~/work/common.tags” will be used.

If one of the items in the list begins with “:”, it will be treated as a
command (other than tag file):

let g:ProjTags += [[ "~/work/proj3", "~/work/proj3/tags",
":set shiftwidth=4" ]]

In this case for all files match the pattern “~/work/proj3/*”, the tag file
“~/work/proj3/tags” will be used, and “shiftwidth=4″ will be set.

Please note that if you use “set” in the command, we will actually use
“setlocal” instead, to avoid polluting the whole vim environment.

You can add the above codes into your vimrc file

为什么要设置一个带绝对路径的tags值呢?因为,如果在/proj目录下面生成了tags文件,在/proj目录时,用vim打开文件,自动是能够找到标记。
如果进入其子目录呢?此时由于其子目录下面并没有tags文件,因此,源码里面的标记便无法交叉引用了。。。

在没有看到这个插件前,我用过这种办法:
首先,.vimrc里面加一句:

set tags=./tags,tags,$HOME/.tags/tags

写一脚本生成当前目录及其子目录下指定文件的tags文件:

[admin@huangye scripts]$ cat gen_tags.sh 
#! /bin/sh
# vars
PRJ_DIR=`pwd`
PRJ_TAG_FILE=${PRJ_DIR}/tags
LIST_FILES=${PRJ_DIR}/tags.list

FIND=/usr/bin/find
CTAGS="/usr/local/bin/ctags --sort=yes --totals=yes --verbose --tag-relative=no"
CSCOPE=/usr/local/bin/cscope

# find files
cd /
${FIND} ${PRJ_DIR} -type f -name "$1" > ${LIST_FILES}
cd -
# generate tag file
${CTAGS} -f${PRJ_TAG_FILE} -L${LIST_FILES}
${CSCOPE} -bq -fcscope.out -i${LIST_FILES} 
echo done.

生成完后,如果要使用当前项目下的tags文件,则把此tags文件copy到~/.tags/tags,可以在.bashrc里面加一个别名:

alias cptags='cp -v `pwd`/tags ~/.tags/tags'

这样也能实现上面插件所实现的功能。当然,有个不好的地方,如果要同时进行两个项目,这种方法就不行了。。。

Tagged in :

All Comments (0)
Gravatar image
No Comments