Last time I checked, a long while ago, ctags
was okay, but it missed some identifiers, and cscope
had a more exhaustive search option, but it still missed some identifiers.
grep
is what I prefer. It's exhaustive but still pretty fast if the source code base is a reasonable size or the PC is fast. You can invoke it from vim with :grep
. Here's a simplified version of my own _vimrc
to show how I have it configured. I put comments to explain what keys do what.
" for Windows, replace with the location of `grep.exe'
" not needed for Unix/Linux
:set grepprg=c:\prog\cygwin\bin\grep.exe\ -n\ $*\ /dev/null
:function! RecursiveSearchIdentifierUnderCursor()
: sp
: let grep_cmd = "silent grep -r \"\\<" . expand("<cword>") . "\\>\" *"
: exe grep_cmd
:endfunction
" CTRL-backslash to search for the identifier (or "word") under the cursor
"
" an identifier is in the form "xxx_123_yyy" but something in the form
" "xxx_123_yyy.aaa_999" would be two identifiers separated by a period
"
"
" this basically executes:
"
" :grep -r "\<identifier\>" *
"
" for whatever `identifier' the cursor happens to be over
"
"
" to close the newly opened window, use:
"
" :q
"
:map <C-\> :call RecursiveSearchIdentifierUnderCursor()<CR>
" cursor up/down to move to the previous/next search result
:map <Up> :cp<CR>zz
:map <Down> :cn<CR>zz
" jump up/down between horizontally split windows
:map <PageUp> <C-W>k
:map <PageDown> <C-W>j
" move to the previous/next enclosing brace
:map <Left> [{
:map <Right> ]}
" move to the previous/next enclosing parenthesis
:map <Home> [(
:map <End> ])
" move to the previous/next enclosing # preprocessor directive
:map <Insert> [#
:map <Del> ]#
Also, don't forget *
(SHIFT-8) and #
(SHIFT-3) to search forwards and backwards for the current identifier under the cursor within the current file. I've found these invaluable when jumping around through code, especially when searching for short variable names like 'i' and 'j' that would otherwise match parts of other variable names in a blind search in a conventional text editor.
You can also invoke :make
from vim
if you like to use make
. It will stop on the first compilation error, then you can edit and start the build again from inside vim
.
Once you get comfortable defining your own functions and key mappings, there are lots of ways to make repetitive programming tasks easier with vim
.