Vim/ctags/cscope tips
Inserting Escape (^[) character
Ctrl -
v -
[
Searching for C string constants in Vim
A simple, but incomplete, regular expression for C string constant is
\"[^"]*\"
It is incomplete because it can't handle ''
\" '' in the string. Shown below is more refined version.
\"\([^"]*\\\"\)*[^"]*\"
For example, to find all C string and embrace them with
S_S(), e.g.,
"Hello\n" to
S_S("Hello\n"),
:1,$s/\"\([^"]*\\\"\)*[^"]*\"/S_S(&)/gc
Inversely, to remove the
S_S() annotations,
:1,$s/S_S(\(\"\([^"]*\\\"\)*[^"]*\"\))/\1/gc
Generating ctags tag for Linux kernel
$ ctags -R --exclude=arch/powerpc --exclude=arch/ia64
--exclude=include/asm-powerpc --exclude=include/asm-ia64
To use tags when in a sub directory, add the line below to .vimrc:
set tags=./tags, ./../tags., ./../../tags, ./../../../tags, ./../../../../tags
Using cscope with Vim
To generate a cscope database, in the source root directory execute
$ cscope -Rbkq
, where
-R is to mean recursive,
-b is to just build the database,
-q is to create ''inverted index'', and
-k is to indicate kernel mode so that no files in
/usr/include is followed.
This command will create
cscope.in.out,
cscope.out, and
cscope.po.out files in the source root.
To update the database later, just execute
$ cscope -U
"
cs " is the Vim command for cscope operations. To add a cscope db,
: cs add path/to/cscope.out