安装 CScope:
sudo apt-get install cscope
确认 vim 支持 cscope:
vim --version | grep cscope
给源代码建立索引:
cscope -Rbq
其中,ctags 递归的在每个目录下生成 tags 文件,供 vim 读取;cscope 生成 cscope.outcscope.out: cscope reference data version 15 with inverted index
将以下内容添加到 ~/.vimrc 中,以自动加载 cscope.out。
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
if has("cscope")
set csprg=/usr/bin/cscope "指定用来执行 cscope 的命令
set csto=1 "先搜索tags标签文件,再搜索cscope数据库
set cst "使用|:cstag|(:cs find g),而不是缺省的:tag
set nocsverb "不显示添加数据库是否成功
" add any database in current directory
if filereadable(ncscope.out")
cs add cscope.out "添加cscope数据库
endif
set csverb "显示添加成功与否
endif
nmap s :cs find s =expand("")
nmap g :cs find g =expand("")
nmap c :cs find c =expand("")
nmap t :cs find t =expand("")
nmap e :cs find e =expand("")
nmap f :cs find f =expand("")
nmap i :cs find i ^=expand("")$
nmap d :cs find d =expand("")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
根据上面的 .vimrc,使用简单的组合键和字母即可使用 cscope 的查找功能。
例如,g 先按 ctrl+@,再按 g,即可查看当前光标所在符号的定义。
:cs helps: 查找 C 语言符号,即查找函数名、宏、枚举值等出现的地方
g: 查找函数、宏、枚举等定义的位置,类似 ctags 所提供的功能
d: 查找本函数调用的函数
c: 查找调用本函数的函数
t: 查找指定的字符串
e: 查找 egrep 模式,相当于 egrep 功能,但查找速度快多了
f: 查找并打开文件,类似 vim 的 find 功能
i: 查找包含本文件的文件