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.
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
ctags in base.
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.
ctags -d -t $( find $root_of_tree -name \*.c -o -name \*.h )will create file
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
SOURCEDIRS=/some/where:/else/where:... cscope -b -Rwill create file
(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.)
:help ctagsfor more info - this here is not complete by far
/.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.)
^]and jump back using
:h tagsrch.txtif interested)
:help cscopefor more info
:cs add /some/where/cscope.out.
cscopetagis 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
cscopetag as hinted above
cscope.out in current dir or environment-given dir.
-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,
set some convenient mappings: <Enter>+letter to issue queries.
See either Vim or Cscope docs (or the comment, above) for different types of queries.