vim + cscope + ctags + taglist

    Source Insight是Windows下最方便浏览代码的工具。但是Source Insight是没有Linux版本的。为了方便在Linux下浏览代码并进行学习,可以利用Vim配合Cscope + Ctags + Taglist来打造Linux下的Source Insight。

Cscope

Cscope是Vim适用的工具和插件,通过Cscope可以方便地获知某个函数的定义以及被哪些函数调用。

安装

可以在http://cscope.sourceforge.net/下载源码包,然后解压,编译安装。

./configure
make
make install

生成cscope数据库

使用cscope前,必须为代码生成一个cscope数据库。假设当前代码在/usr/src/linux目录下,则运行下列命令。

cd /usr/src/linux
cscope –Rbq

然后会生成3个文件:cscope.in.out,cscope.out,cscope.po.out。

用vim打开代码文件,将刚才生成的cscope文件导入到vim中。

vim init/main.c
:cs add /usr/src/linux/cscope.out /usr/src/linux

也可以将下面语句添加到vim的配置文件.vimrc中。

if fileradable("cscope.out")
    cs add csope.out
elseif $CSCOPE_DB  != ""
    cs add $CSCOPE_DB
endif

Cscope的功能

Cscope的功能通过它的子命令“find”来实现。

cs find c|d|e|g|f|i|s|t name

  • s:查找C代码符号
  • g:查找本定义
  • d:查找本函数调用的函数
  • c:查找调用本函数的函数
  • t:查找本字符串
  • e:查找本egrep模式
  • f:查找本文件
  • i:查找包含本文件的文件

可以在.vimrc中添加下面的快捷键,免得每次都要输入一长串命令。

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("<cword>")<CR><CR>
nmap <C-@>i :cs find i ^<C-R>=expand("<cword>")<CR>$<CR>
nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>

使用时,将光标停留在要查找的对象上,按下<C-@>g,即先按“Ctrl+@”,然后很快再按“g”,将会查找该对象的定义。

Ctags

ctags -R

Example:

su -

在/usr/include 下执行ctags -R生成一个标签文件tags

vim /etc/vim/vimrc添加 set tags=/usr/include/tags

使用vim打开源文件,把光标放在关键词上

按ctrl  + ] 查找,按ctrl + t 返回。


Taglist

能够列出原文件中的tag(macro、variable、function、class,etc)并跳转。由于taglist依赖于ctags,所以要先安装ctags,否则taglist装了也没法用。

1、下载taglist

http://www.vim.org/scripts/script.php?script_id=273 官方网站上下载taglist安装包。

2、搭建步骤

install details

2.1. Download the taglist.zip file and unzip the files to the $HOME/.vim or the
    $HOME/vimfiles or the $VIM/vimfiles directory. After this step, you should
    have the following two files (the directory structure should be preserved):

         plugin/taglist.vim - main taglist plugin file
         doc/taglist.txt    - documentation (help) file

   Refer to the |add-plugin|, |add-global-plugin| and |runtimepath| Vim
   help pages for more details about installing Vim plugins.
2.2. Change to the $HOME/.vim/doc or $HOME/vimfiles/doc or $VIM/vimfiles/doc
    directory, start Vim and run the ":helptags ." command to process the
    taglist help file. Without this step, you cannot jump to the taglist help
    topics.
2.3. If the exuberant ctags utility is not present in your PATH, then set the
    Tlist_Ctags_Cmd variable to point to the location of the exuberant ctags
    utility (not to the directory) in the .vimrc file.
2.4. If you are running a terminal/console version of Vim and the terminal
    doesn't support changing the window width then set the
    'Tlist_Inc_Winwidth' variable to 0 in the .vimrc file.
2.5. Restart Vim.
2.6. You can now use the ":TlistToggle" command to open/close the taglist
    window. You can use the ":help taglist" command to get more information
    about using the taglist plugin.

3、配置 /etc/vim/vimrc

let Tlist_Ctags_Cmd="/usr/bin/ctags"
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Use_Left_Window=1
let Tlist_Auto_Open=1


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值