Ctags, Cscope and Vim

本文介绍了如何在Arch Linux和NetBSD上安装及使用Ctags和Cscope来增强Vim的源码浏览和导航功能。Ctags和Cscope分别用于生成源文件的符号索引,Cscope还提供了基于ncurses的界面。通过在Vim中整合这两个工具,可以极大地提高开发者的生产力。文章详细阐述了每个工具的安装步骤、数据库创建方法以及在Vim中的使用技巧,包括快捷键映射,以方便进行符号查找和定义跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ctags and expecially Cscope in combination with Vim can really boost productivity when working with home-grown and foreign source-trees.

Both tools basically generate references out of source-files, which can be used to locate and cross-reference source-symbols.

While Cscope does come with its own curses-based UI, Vim provides a really nice interface to either tool.

Installation
Ctags
In Arch Linux, package ctags is actually Exuberant Ctags. The binary is called ‘ctags’.

In NetBSD, you can use ctags in base, or Exuberant Ctags (‘exctags’ in pkgsrc). The latter generates more tags; e.g. simple C functions are not (always?) found using the ctags in base.

Cscope
in Arch Linux, package cscope is in the standard repository. From what I can see, it’s available as NetBSD package as well.

As far as I can see, there is just 1 flavour of Cscope.

Create database
vanilla Ctags (NetBSD base)
go to the dir you want to work in (does not have to be the root of a project-source)
ctags -d -t $( find $root_of_tree -name *.c -o -name *.h ) will create file tags
Exuberant Ctags
go to the dir you want to work in
exctags $( find $root_of_tree -name *.c -o -name *.h )
Helper-script to generate tags for C-files in multiple given dirs (recursive):

#!/usr/bin/bash
    
dirs=$*
[ -n "$dirs" ]  ||  dirs=.
    
rm -f tags
    
for d in $dirs; do
    ctags -a $( find $d -name \*.c -o -name \*.h )
done

Cscope
go to the dir you want to work in
SOURCEDIRS=/some/where:/else/where:… cscope -b -R will create file cscope.out
(For non-native source-trees, -k (‘kernel’) can be given to exclude system include-dirs - ‘/usr/include’ etc - from trying to locate #included files.)

(To generate ASCII-only cscope.out files, add the -c switch. This makes it easier to debug Cscope-lookup, when e.g. lookup of a symbol fails.)

Use in Vim
Ctags
see :help ctags for more info - this here is not complete by far
set ‘tags’ (:help tags-option) to /.tags,tags,~/tags. This searches in the current file’s dir first, then in the dir from which Vim was started, then in the home-dir. (By default, ‘tags’ may not include the start-dir as location.)
jump to definition using ^] and jump back using ^o
(:h tagsrch.txt if interested)
Cscope
see :help cscope for more info
if necessary, manually add generated database-files using :cs add /some/where/cscope.out.
if cscopetag is set, all/most tag-related commands will use Cscope-tags instead of ctag ones.
To automate things a bit, I use the following snippet in .vimrc (as found somewhere on the net - thank you, original author):

if has("cscope")
    set csprg=/usr/bin/cscope
    set csto=0
    set cst
    set nocsverb
    " add any database in current directory
    if filereadable("cscope.out")
        cs add cscope.out . -C                            (*)
    " else add database pointed to by environment
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB . -C                            (*)
    endif
    set csverb
    " 0 or s: Find this C symbol
    " 1 or g: Find this definition
    " 2 or d: Find functions called by this function
    " 3 or c: Find functions calling this function
    " 4 or t: Find this text string
    " 6 or e: Find this egrep pattern
    " 7 or f: Find this file
    " 8 or i: Find files #including this file
      nmap <C-m>s :cs find s <C-R>=expand("<cword>")<CR><CR>
      nmap <C-m>g :cs find g <C-R>=expand("<cword>")<CR><CR>
      nmap <C-m>c :cs find c <C-R>=expand("<cword>")<CR><CR>
      nmap <C-m>t :cs find t <C-R>=expand("<cword>")<CR><CR>
      nmap <C-m>e :cs find e <C-R>=expand("<cword>")<CR><CR>
      nmap <C-m>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
      nmap <C-m>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
      nmap <C-m>d :cs find d <C-R>=expand("<cword>")<CR><CR>
endif

…which basically does the following:

prefer Cscope DB over ctags tags if both are present

set cscopetag as hinted above

add cscope.out in current dir or environment-given dir.

(*): the -C flag (search case-insensitive) and dummy . dir necessary to add that flag. Case-insensitive search is what you (or at least I) always want, really.

set some convenient mappings: +letter to issue queries.

See either Vim or Cscope docs (or the comment, above) for different types of queries.

Have fun!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值