How to list all occurrence of a identifier

32 views
Skip to first unread message

Steven Woody

unread,
Nov 24, 2008, 12:37:59 AM11/24/08
to Vim
Hi,

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

Tony Mechelynck

unread,
Nov 24, 2008, 1:15:36 AM11/24/08
to vim...@googlegroups.com, Vim

: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.

Tony Mechelynck

unread,
Nov 24, 2008, 1:15:36 AM11/24/08
to vim...@googlegroups.com, Vim
On 24/11/08 06:37, Steven Woody wrote:

:vimgrep /\<foo\>/g ./**/*.[ch] ./**/*.cpp

Dominique Pelle

unread,
Nov 24, 2008, 1:39:30 AM11/24/08
to vim...@googlegroups.com
2008/11/24 Steven Woody <narke...@gmail.com>:

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

Tom Link

unread,
Nov 24, 2008, 2:14:31 AM11/24/08
to vim_use
> 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).

Plug: Besides cscope & vimgrep, which were already mentioned, for
smaller projects you could also use trag[1], which works as a wrapper
around vimgrep most of the time but does the regexp building and file
handling for you -- if properly set up.

E.g. :Trag f foo

It currently doesn't explicitly support C/C++ though but I assume the
general patterns would work for finding function calls.


[1] http://www.vim.org/scripts/script.php?script_id=2033

Yegappan Lakshmanan

unread,
Nov 24, 2008, 1:33:51 PM11/24/08
to vim...@googlegroups.com, Vim
Hi,

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

Yegappan Lakshmanan

unread,
Nov 24, 2008, 1:33:51 PM11/24/08
to vim...@googlegroups.com, Vim
Hi,

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

yosi izaq

unread,
Nov 25, 2008, 5:35:09 AM11/25/08
to vim...@googlegroups.com
Hi,

Great tips guys. I have a few Qs plz.

Tony said
"
:vimgrep /\<foo\>/g ./**/*.[ch] ./**/*.cpp
"

I find this very useful as it works right off the bat. Is there anyway
I can shortcut to it, either by mapping or by adding a customized menu
item (for GUI). Either of them should prompt for the pattern to search
(and this is actually what I don't know how to do).
So for menu I'd like for example:
amenu MyMenu.SearchPattern :ask for pattern and do :vimgrep
/\<pattern\>/g ./**/*.[ch] ./**/*.cpp


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.


Yegappan, Are there any "tutorials" on how to install and setup
id-utils on Linux?


TIA all,
Yosi

yosi izaq

unread,
Nov 25, 2008, 5:39:56 AM11/25/08
to vim...@googlegroups.com
Oh and regarding vimgrep.

Any idea how to get rid of E303 error "unable to open swap file for..." ?

10x,
Yosi

Dominique Pelle

unread,
Nov 25, 2008, 6:41:19 AM11/25/08
to vim...@googlegroups.com
2008/11/25 yosi izaq <iza...@gmail.com>:

> 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

yosi izaq

unread,
Nov 25, 2008, 8:12:20 AM11/25/08
to vim...@googlegroups.com
"> 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"
"
Yes this works well. What didn't was the search for the locations from
which a method is called. It has returned no results when it should
have. Now, I've made some more tests and recreated the scope DB and
now it seems to work (Meaning I probably did something wrong before)
but still I can't get the results to go the quickfix (or to get any
kind of listing of matches in any way possible for that matter).

Note - I've run the following from the ex prompt:
set cscopeverbose
set cscopequickfix=s-,c-,d-,i-,t-,e-,g-

10x,
Yosi

yosi izaq

unread,
Nov 25, 2008, 8:35:47 AM11/25/08
to vim...@googlegroups.com
ok. Please ignore my previous response. The cscope query now works
perfectly and lists matches to quickfix.

As for the GNU ID Utils, I've started to read several online documents
but couldn't find a basic mkid scan command for C++ project.
I've made some tests on windows (to avoid the Linux install hassle)
but get the following error:
c:\views\<my_project_path>\ mkid *.[chy]
mkid: can't get working directory: Permission denied

Yosi

yosi izaq

unread,
Nov 25, 2008, 9:05:48 AM11/25/08
to vim...@googlegroups.com
Sorry 4 so many questions.
Its just that those options are very useful and I'm trying to get them
to work well on my system.

So, another Q plz: How do I change this - if
filereadable(expand("$HOME/cscope.out")) -
To check in the directory from which VIM was started instead of $HOME?

TIA,
Yosi

Ben Schmidt

unread,
Nov 25, 2008, 9:22:31 AM11/25/08
to vim...@googlegroups.com
> So, another Q plz: How do I change this - if
> filereadable(expand("$HOME/cscope.out")) -
> To check in the directory from which VIM was started instead of $HOME?

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.

Charles Campbell

unread,
Nov 25, 2008, 10:44:53 AM11/25/08
to vim...@googlegroups.com
The cecscope.vim plugin does quickfix with cscope. You can get a copy
from my website:
http://mysite.verizon.net/astronaut/vim/index.html#CECSCOPE

Regards,
Chip Campbell

yosi izaq

unread,
Nov 25, 2008, 10:53:50 AM11/25/08
to vim...@googlegroups.com
> 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"))

I needed the first. It works well. Thanks!

> 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).
Sure. Is this reply according to the preference?

10x,
Yosi

Steven Woody

unread,
Nov 25, 2008, 11:16:19 AM11/25/08
to vim...@googlegroups.com

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.

Gary Johnson

unread,
Nov 25, 2008, 8:16:23 PM11/25/08
to vim...@googlegroups.com
On 2008-11-25, yosi izaq <iza...@gmail.com> wrote:
> > 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"))
>
> I needed the first. It works well. Thanks!
>
> > 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).
> Sure. Is this reply according to the preference?

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

yosi izaq

unread,
Nov 26, 2008, 9:04:39 AM11/26/08
to vim...@googlegroups.com

>> > 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).
>> Sure. Is this reply according to the preference?
>
> 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.

I wasn't aware of the "Auto" quoting of the original message. In-fact I just found out about the "show quoted text-" thingi that actually shows it --I think by now I got it right.
Thank you for taking the time to walk me through the preferred standard here.

Yosi

Chris Bannister

unread,
Dec 5, 2008, 7:58:29 AM12/5/08
to vim...@googlegroups.com
On Wed, Nov 26, 2008 at 04:04:39PM +0200, yosi izaq wrote:
> I wasn't aware of the "Auto" quoting of the original message. In-fact I just
> found out about the "show quoted text-" thingi that actually shows it --I
> think by now I got it right.

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

Reply all
Reply to author
Forward
0 new messages