1)在Vim下运行version查看Vim支持哪些特性,前面有前缀符号+的为支持。如果支持Cscope,则直接进入2),否则下载Cscope源代码包编译安装。
2)确定Vim已支持Cscope后,去网上找个cscope_maps.vim文件,可×××http://cscope.sourceforge.net/cscope_maps.vim下载。拷贝到~/.vim/plugin目录。
到这里,我们就可以开始使用Cscope了。
1)使用Cscope需要生成cscope数据库文件。进入项目代码根目录运行命令: copy
cscope -Rbq -f path/xxx.out
命令运行后会生成xxx.out文件,即cscope数据库文件。更多用法参考man cscope文档。
2)进入项目代码根目录,在Vim下运行命令:
cs add path/xxx.out
此命令将cscope数据库载入Vim。
3)Cscope常用快捷键
Ctrl-\ s 查找所有当前光标所在符号出现过位置。
Ctrl-\ c 查找所有调用当前光标所在函数的函数。
按下快捷键查找结束后会在编辑区下方出现查找结果的列表,输入结果编号并回车,就能跳转到该查找结果在源代码中的相应位置。例如,我们将光标移到initial_pool_size变量定义的位置,即17行,然后按下"Ctrl-\ s"组合快捷键,得到示图如下:
然后我们输入2,并回车,就能跳转到第2个查找结果。
为了界面更好看,可以把Cscope的查找结果输出到quickfix窗口,需要在~/.vimrc中加入下面这行:
set cscopequickfix=s-,c-,d-,i-,t-,e-
这样,通过快捷键查找某个符号后,会立即跳转到第一个找到的该符号出现的位置。如果你对这次默认跳转的位置不满意,在Vim命令行下运行cw命令,就能在编辑区下面quickfix窗口看到所有查找结果的列表,点击相应列表项就能跳转到相应位置。这个功能已经跟VS很接近了吧:)
然而:
由于在vim中:cs add XXX/cscope.out使用的是相对路径,这会导致查找符号后没法打开文件。解决方法如下:
一个脚本,可以实现在项目的任一子目录下,自动的向上查找cscope.out,并把他add进来。
把这个脚本命名cs.sh,放在要代码目录下。
#!/bin/sh
find . -name "*.h" -o -name "*.c" -o -name "*.cpp" -o -name "*.java" > cscope.files
cscope -bkq -i cscope.files
ctags -R
但这不解决cs add时找不到路径问题,所以需要修改~/.vimrc,加入如下片段:
if has("cscope")
set csprg=/usr/bin/cscope
set csto=0
set cst
set csverb
set cspc=3
"add any database in current dir
if filereadable("cscope.out")
cs add cscope.out
"else search cscope.out elsewhere
else
let cscope_file=findfile("cscope.out", ".;")
let cscope_pre=matchstr(cscope_file, ".*/")
if !empty(cscope_file) && filereadable(cscope_file)
exe "cs add" cscope_file cscope_pre
endif
endif
endif
最后贴出目前完整.vimrc
let Tlist_Auto_Open=1
let Tlist_Show_One_File=1
set autochdir
set tags=tags;
set ts=4
set expandtab
set cscopequickfix=s-,c-,d-,i-,t-,e-
let NERDTreeWinPos='right'
noremap
noremap
noremap
if has("cscope")
set csprg=/usr/bin/cscope
set csto=0
set cst
set csverb
set cspc=3
"add any database in current dir
if filereadable("cscope.out")
cs add cscope.out
"else search cscope.out elsewhere
else
let cscope_file=findfile("cscope.out", ".;")
let cscope_pre=matchstr(cscope_file, ".*/")
if !empty(cscope_file) && filereadable(cscope_file)
exe "cs add" cscope_file cscope_pre
endif
endif
endif