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!