关于VIM+ctags+cscope安装与使用

安装ctags和taglist

$ sudo apt-get install cscope
$ sudo apt-get install ctags

另外需要安装vim的插件taglist, taglist插件是以vim脚本的形式存在的,因此只需要将其下载下来放到相应的目录即可。
可以去VIM的官方网址,上面有安装方法:
install details
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. 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.
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.
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.
5. Restart Vim.
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.
简单解释一下上述步骤:
(1)安装taglist
以taglist_46.zip为例,下载后解压至$HOME/.vim路径下,使用unzip命令:
-d选项表示解压到指定目录(默认解压到当前目录),解压后可以看到有两个文件夹:

$ unzip taglist_46.zip -d $HOME/.vim
Archive:  taglist_46.zip
  inflating: /home/zfchen/.vim/plugin/taglist.vim  
  inflating: /home/zfchen/.vim/doc/taglist.txt 

然后进入$HOME/.vim/doc目录下,打开Vim($ vim),执行”:helptags .“命令,此步骤是将doc下的帮助文档加入到Vim的帮助主题中,这样我们就可以在Vim中运行“:help taglist.txt”查看taglist的帮助文档。
(2)配置taglist
关于具体配置项的说明和快捷键的使用可以参考这篇blog,~/.vimrc脚本的内容可以参考另外一个链接,下面是我使用的脚本内容。

"设置ctags路径
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
"启动vim后自动打开taglist窗口
let Tlist_Auto_Open = 1
"taglist为最后一个窗口时,退出vim
let Tlist_Exit_OnlyWindow = 1
"总是使用鼠标
let mouse=a
"单击tag跳转
let Tlist_Use_SingleClick = 1
"--ctags setting--
" 按下F5重新生成tag文件,并更新taglist
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
set tags+=~/my_repository/linux/tags "add new tags file

另外可以使用 Ctrl+WW组合键(按2次W),在标签区和代码区进行切换。
可以使用:Tlist命令打开或关闭tag区。

关于cscope的配置

同样需要在~/.vimrc脚本中添加一些配置项,如下:

if has("cscope")
  set csprg=/usr/bin/cscope
  set csto=1
  set cst
  set nocsverb
  " add any database in current directory
  if filereadable("cscope.out")
      cs add cscope.out
  endif
  set csverb
endif

关于上面的cscope.out文件,可以设置为某个指定的目录(比如linux内核的目录: “/home/zfchen/my_repository/linux/cscope.out”)
cscope的用法:
(1)首先需要为你的代码生成一个cscope数据库
在你的项目根目录运行下面的命令:

cscope -Rbq 

这个命令会生成三个文件:cscope.out, cscope.in.out, cscope.po.out。其中cscope.out是基本的符号索引,后两个文件是使用”-q“选项生成的,可以加快cscope的索引速度。Cscope只在第一次解析时扫描全部文件,以后再调用cscope,它只扫描那些改动过的文件,这大大提高了cscope生成索引的速度。
这里列出了cscope的常用选项:

-R: 在生成索引文件时,搜索子目录树中的代码
-b: 只生成索引文件,不进入cscope的界面
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
-k: 在生成索引文件时,不搜索/usr/include目录
-i: 如果保存文件列表的文件名不是cscope.files时,需要加此选项告诉cscope到哪儿去找源文件列表。可以使用“–”,表示由标准输入获得文件列表
-Idir: 在-I选项指出的目录中查找头文件
-u: 扫描所有文件,重新生成交叉索引文件
-C: 在搜索时忽略大小写
-Ppath: 在以相对路径表示的文件前加上的path,这样不用切换到数据库文件所在的目录也可以使用它了。

(2)在Vim中连接到指定的数据库
打开Vim,使用cs add cscope.out命令连接指定的数据库。快捷键使用——参考链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值