【vim】cscope插件

版权声明:本文为博主原创文章,转载需注明出处,谢谢合作 https://blog.csdn.net/QQ2010899751/article/details/82532272

环境:ubuntu 18.04

           vim version 8.0.1453

1、安装cscope插件

$ sudo apt install cscope

2、操作步骤

  • 进入源码根目录下,在shell终端执行命令,生成 cscope数据库 cscope.out
$ cscope -Rqkb
  • 在VIM的命令行模式,添加 cscope数据库
: cs add cscope.out

        或 在/etc/vim/vimrc 添加cscope数据库
        或 在/etc/vim/vimrc.local 添加cscope数据库(推荐)
        或 在~/.vimrc 添加cscope数据库

  • 在VIM的命令行模式,进行查找<name>
 : cs find <type> <name> 

        或 使用/etc/vim/vimrc.local nmap快捷键(推荐)

3、相关说明

  • 执行 cscope -Rkqb 后,会生成成三个文件:

        cscope.out, cscope.in.out, cscope.po.out.
        其中cscope.out是基本的符号索引,
        后两个文件是使用"-q"选项生成的,可以加快cscope的索引速度。
        

  • cscope -Rkqb

        R : 在生成cscope数据库时,搜索子目录树中的代码
        q : 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
        k : Cscope在生成数据库中,在你的源码根目录中未找到的头文件,
            会自动到/usr/include目录中查找。
            如果你想阻止它这样做,使用"-k"选项。
        b : 在缺省情况下,cscope在生成数据库后就会进入它自己的查询界面,
            使用"-b"选项,不进入。
            

  • 在 /etc/vim/vimrc.local 中添加配置
"""""""""""""""""""""""cscope set""""""""""""""""""      
if has("cscope")    
	set csprg=/usr/bin/cscope    

    "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    
  
nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>  
nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR> 
nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>  
nmap <C-@>s :cs find s <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-@>i :cs find i <C-R>=expand("<cfile>")<CR><CR>  
  • VIM支持8种cscope的查询功能,如下:

        cs find c <name> : 查找调用本函数的函数
        cs find d <name> : 查找本函数调用的函数
        cs find g <name> : 查找函数、宏、枚举等定义的位置,类似ctags的功能
        cs find s <name> : 查找C语言符号,即查找函数名、宏、枚举值等出现的地方
        cs find t <name> : 查找指定的字符串,如printk()打印在串口的字符串。
        cs find e <name> : 查找egrep模式,相当于egrep功能,但查找速度快多了
        cs find f <name> : 查找并打开文件,类似vim的find功能
        cs find i <name> : 查找包含本文件的文件
    

  • 在VIM的命令模式,得到帮助。
: cs help

猜你喜欢

转载自blog.csdn.net/QQ2010899751/article/details/82532272