1 安装
pacman -S global。
2 配置
2.1 vimrc配置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" gscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"gtags.vim 设置项
let GtagsCscope_Auto_Load = 1
let CtagsCscope_Auto_Map = 1
let GtagsCscope_Quiet = 1
if has("cscope")
if MySys() == "linux"
set csprg=/usr/bin/gtags-cscope
else
set csprg=gtags-cscope
endif
set csto=1
set cst
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add cscope.out
endif
"cs add /home/kernel/linux-4.9/cscope.out /home/kernel/linux-4.9
set csverb
endif
"查找C语言符号,即查找函数名、宏、枚举值等出现的地方
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
"查找调用本函数的函数
nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
"查找指定的字符串
nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
"查找egrep模式,相当于egrep功能,但查找速度快多了
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
"查找本文件,类似vim的find功能
nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
"查找包含本文件的文件
nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
"查找本函数调用的函数
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
nmap <C-LeftMouse> :tag <C-R>=expand("<cword>")<CR><CR>
3 使用
在项目根目录执行gtags命令,会生成三个文件:GPATH GRTAGS GTAGS。
然后进入项目根目录或子目录内,vim打开对应文件,ctrl+]看有无反应。
4 遇到的问题
4.1 gtags默认使用vi
解决方法:
在~/.bashrc里面设置默认编辑器
#设置默认编辑器为vim,解决gtags默认使用vi问题
export EDITOR=vim
export VISUAL=vim
4.2 vim跳转时提示对应文件不存在
原因: vim打开之后查看pwd,发现vim自己切路径到文件的当前路径了。而gtags记录的路径是相对于打开文件前的路径的。
解决方法: 把vimrc 里的set autochdir换成set noautochdir
参考文章鸣谢:
【1】使用vim+gtags阅读内核源码 —作者:PaulForCoding
https://blog.youkuaiyun.com/weixin_42628255/article/details/137693694
【2】在Vim中使用gtags ----作者:狂神314
https://www.cnblogs.com/kuang17/p/9449258.html