vim的cscope _ ctags配置

概述

我们经常阅读源码,需要查看函数实现,结构体定义和接口调用等。

常见的工具有:

  • sourceinsight
  • vim
  • vscode
  • opengrok

自己常用的就是vim和opengrok: vim配置后比较跟手, 主要用来中小范围内的阅读代码; opengrok主要用来全工程搜索。两者完美互补。

cscope + ctags:不依赖编译就可以生成数据库,但在查看时会误报。
lsp:依赖编译才能生成数据库,在查看时很准确。

下文就说下自己cscope + ctags的配置方式

工具安装

sudo apt-get install cscope
sudo apt-get install universal-ctags

cli 适配

~/bin/qk.sh // 自定义工具的使用

qtag(){
    cs_bs=~/.cscope_bs
    find `pwd` -name "*.c" -o -name "*.h" > ls.files && cscope -Rbkq -i ls.files && ctags -R
    [ -d ${cs_bs} ] && cp -rf cscope.out ${cs_bs}
}

bash/zsh 配置

~/.zshrc

[ -f ~/bin/qk.sh ] && source ~/bin/qk.sh

nvim 配置

~/.config/nvim/init.vim //自动添加数据库;绑定快捷键

set tags=tags   
if has("cscope")
    set csto=1 
    set cst
    if filereadable("cscope.out")
        cs add cscope.out
    endif
endif
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>
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>

使用方法

cd xxx // 进入代码目录
qtag   // 执行定义函数, 创建数据库
nvim   // 开启nvim后阅读源码

效果如下:
image

posted @ 2022-06-04 11:30  whilewell  阅读(507)  评论(0编辑  收藏  举报