I am using vim writing C/C++ and I want to (as one usually can in
other IDE) have vim listed all occurrence of identifier. For example,
there is a method foo() in a class C, I want to get know where the
foo() is called in the whole project (current directory and its
subdirectories). It seems, in this case, a ctag tags file does not
help since it can only list where the identifier is defined/declared.
Thanks in advance.
-
narke
:vimgrep /\<foo\>/g ./**/*.[ch] ./**/*.cpp
will find every occurrence of foo (as a word) in files *.c *.h and *.cpp
in the current directory or below up to 30 levels down. The results
appear as a quickfix list, so the usual commands apply:
- to see the matches (editing the files they're in)
:cfirst
:cnext
:cprev
:clast
:cnfile
:cpfile
- to see one line per match in the quickfix window
:copen
:cclose
Of course you can map those you use most to keys, for instance my vimrc
includes
map <F2> :cnext<CR>
map <S-F2> :cprev<CR>
Note: Since :vimgrep does a "dumb" search using a Vim search pattern, it
will list _all_ occurrences of the word, also as part of a string or
comment.
Best regards,
Tony.
--
There's an old proverb that says just about whatever you want it to.
:vimgrep /\<foo\>/g ./**/*.[ch] ./**/*.cpp
cscope can be useful for this. Install cscope, then build a cscope DB
(cscope.out) from your source files. Connect to the cscope DB in Vim
with :cs add cscope.out (you can put it in your ~/.vimrc) then you can
start using cscope in Vim. For example, to find all locations where foo()
is called, you can do:
:cs find c foo
Then use quickfix commands to navigates through all matches
(:cn or :copen for example) assuming you have set up cscopequickfix
as in my ~/.vimrc. Here is a snippet from my ~/.vimrc
(http://dominique.pelle.free.fr/.vimrc):
==================================
if has('cscope')
if filereadable(expand("$HOME/cscope.out"))
cs kill -1
cs add ~/cscope.out
endif
set cscopeverbose
set cscopequickfix=s-,c-,d-,i-,t-,e-,g-
endif
set tags=tags,~/tags,~/stl-tags
==================================
cscope answers those questions:
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find assignments to
Here is a sh script which I use to produce the cscope DB
and ctags (exuberant ctags) file for example:
==================================
#!/bin/sh
TMP_FILE=/tmp/build-tags.$$
find ~/sb/vim7/src \
\( -name '*.l' -o \
-name '*.y' -o \
-name '*.c' -o \
-name '*.cpp' -o \
-name '*.h' \) \
-print > $TMP_FILE
echo "=== Building ctags..."
ctags -L $TMP_FILE --language-force=c++ \
--c++-kinds=+p \
--fields=+iaS \
--extra=+f+q -f ~/tags
echo "=== Building cscope database..."
rm -f cscope.*
cscope -i $TMP_FILE -b -q -f ~/cscope.out
rm -f $TMP_FILE
==================================
It's not perfect, since it can't distinguish between
overloaded c++ functions for example, but still helps
a lot.
-- Dominique
The fastest way to search across a large number of C files is to use
GNU id-utils.
http://www.gnu.org/software/idutils/idutils.html
With GNU id-utils, it takes few seconds to search across several
thousand source files.
You can use the following Vim plugin with GNU id-utils:
http://vim.sourceforge.net/scripts/script.php?script_id=251
- Yegappan
On Sun, Nov 23, 2008 at 9:37 PM, Steven Woody <narke...@gmail.com> wrote:
>
The fastest way to search across a large number of C files is to use
> Dominique suggested cscope. Would it work for methods as well?- I've
> tried that query on a cscope DB and it didn't seem to be able to cope
> well with methods.
It works, I use it daily. I'm not sure what you mean by "didn't seem to be
able to cope well with methods".
But, if you have a big project, we a method called like this...
foo->Set(...);
... doing ":cs find g Set" with cscope, or g<ctrl-]> with ctags when cursor
is on "Set", or ":ts Set" may give many matches since "Set" is such a generic
name, which may be present in many classes. Neither cscope, nor ctags
handle that well. But in most case, if the method name is not too common,
it does the sensible thing. In any case, it's better and an order of magnitude
faster than using ":vimgrep"
-- Dominique
Well, if you just want the current directory, surely just
filereadable("cscope.out")
If really you want the directory of the current buffer, perhaps
filereadable(expand("%:p:h/cscope.out"))
As has just been pointed out in another thread, bottom-posting is
preferred on this mailing list, so it'd be good if you could do that in
your replies (like I have: write your reply under the text you are
replying to, trimming the text so only the relevant parts are left).
Cheers,
Ben.
Regards,
Chip Campbell
Thanks all of you for these information. It seems your answers in
this thread are all of much use. I am still study your methods ... If
met problem, I will be back.
You're halfway there. Since you quoted the relevant parts of the
original message before your replies, there is no need to include
the original message below your reply as well.
Regards,
Gary
Arrrgh, ... that explains a few things ... and not just on this list.
> Thank you for taking the time to walk me through the preferred standard
> here.
It is a good practice to adopt no matter what the preferred standard is.
--
Chris.
======
I contend that we are both atheists. I just believe in one fewer god
than you do. When you understand why you dismiss all the other
possible gods, you will understand why I dismiss yours.
-- Stephen F Roberts