vimgrep

287 views
Skip to first unread message

hilal Adam

unread,
Apr 4, 2012, 1:12:28 PM4/4/12
to v...@vim.org
Sorry if not correct platform for this question.
Need to use vimgrep/grep to find a particular string in c code.  I am trying to find all occurrences of '**' (pointer to pointer). But I get hundreds of lines of output for all comment lines which include a minimum of 2 '*'s.
Any help is appreciated.

HA

Ben Fritz

unread,
Apr 4, 2012, 1:22:19 PM4/4/12
to vim...@googlegroups.com, v...@vim.org, hilal Adam
On Wednesday, April 4, 2012 12:12:28 PM UTC-5, hilal Adam wrote:
> Sorry if not correct platform for this question.</div>
> Need to use vimgrep/grep to find a particular string in c code.  I am trying to find all occurrences of &#39;**&#39; (pointer to pointer). But I get hundreds of lines of output for all comment lines which include a minimum of 2 &#39;*&#39;s.</div>
> Any help is appreciated.</div>
>
> </div>
> HA
> </div></div></div>

vimgrep, just like '/' searching in Vim, uses regular expressions. In a regular expression, * means "as many as possible of the preceding search atom" which normally means "as many as possible of the preceding character".

You don'

Ben Fritz

unread,
Apr 4, 2012, 1:28:25 PM4/4/12
to vim...@googlegroups.com, v...@vim.org, hilal Adam
Dammit, I accidentally hit some key combination which Google Groups used to post before I was done.

You don't give the exact search you used, so I was guessing here that you searched with ** as your search pattern, which will match zero or more '*' characters, but I realize now that this will match absolutely anything, because "as many as possible" means zero or more, so ** will match every line in every file.

Probably, then, you searched for \*\*, which will match 2 '*' characters in a row. However, it will match ANY two * characters, even if followed by another character which is not a * character.

You need to specify that you only want to match where the preceding character and the next character are not also *.

To do this, I'd use a pattern like:

\*\@<!\*\*\*\@!

or simpler, with "very not magic":

\V*\@<!***\@!

See :help /\@<! and :help /\@!

hilal Adam

unread,
Apr 4, 2012, 2:27:21 PM4/4/12
to vim...@googlegroups.com
Ben, Thanks for your prompt response.
Somehow I am getting 3 copies of your premature, and complete replies.

Yes. You're right \*\* was one of the approaches I used. Unfortunately * is treated as a metacharacter/special character/keyword by grep/vimgrep/c language and this where the difficulty is originating from.

I tried your suggestion, \*\@<!\*\*\*\@!, and I get a no pattern found reply while I stirring at a line that has **.

How would you do this in grep on cygwin command line?

Thanks again.


From: Ben Fritz <fritzo...@gmail.com>
To: vim...@googlegroups.com
Cc: "v...@vim.org" <v...@vim.org>; hilal Adam <hil...@yahoo.com>
Sent: Wednesday, April 4, 2012 12:28 PM
Subject: Re: vimgrep

On Wednesday, April 4, 2012 12:22:19 PM UTC-5, Ben Fritz wrote:
> On Wednesday, April 4, 2012 12:12:28 PM UTC-5, hilal Adam wrote:
> > Sorry if not correct platform for this question.</div>
> > Need to use vimgrep/grep to find a particular string in c code.  I am trying to find all occurrences of '**' (pointer to pointer). But I get hundreds of lines of output for all comment lines which include a minimum of 2 '*'s.</div>

> > Any help is appreciated.</div>
> >
> > </div>
> > HA
> > </div></div></div>
>
> vimgrep, just like '/' searching in Vim, uses regular expressions. In a regular expression, * means "as many as possible of the preceding search atom" which normally means "as many as possible of the preceding character".
>
> You don'

Dammit, I accidentally hit some key combination which Google Groups used to post before I was done.

You don't give the exact search you used, so I was guessing here that you searched with ** as your search pattern, which will match zero or more '*' characters, but I realize now that this will match absolutely anything, because "as many as possible" means zero or more, so ** will match every line in every file.

Probably, then, you searched for \*\*, which will match 2 '*' characters in a row. However, it will match ANY two * characters, even if followed by another character which is not a * character.

You need to specify that you only want to match where the preceding character and the next character are not also *.

To do this, I'd use a pattern like:

\*\@<!\*\*\*\@!

or simpler, with "very not magic":

\V*\@<!***\@!

See :help /\@<! and :help /\@!

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php


Jürgen Krämer

unread,
Apr 5, 2012, 1:39:38 AM4/5/12
to vim...@googlegroups.com

Hi,

[please don't top-post]

hilal Adam wrote:
>
> Yes. You're right \*\* was one of the approaches I used. Unfortunately
> * is treated as a metacharacter/special character/keyword by
> grep/vimgrep/c language and this where the difficulty is originating
> from.
>
> I tried your suggestion, \*\@<!\*\*\*\@!, and I get a no pattern found
> reply while I stirring at a line that has **.

Ben's suggestion should work -- at least it did for me. But you can also
try the following pattern

\(^\|[^*]\)\*\*\([^*]\|$\)

This searches for two asterisks that are preceded by either the start of
line or some character other than an asterisk and which are followed by
a non-asterisk or the end of line.

This would also include the preceding and following characters in the
match, if any. If you want to restrict the match (and thus the
highlighting) to the two asterisks, include \zs and \ze in the pattern:

\(^\|[^*]\)\zs\*\*\ze\([^*]\|$\)

Regards,
J�rgen

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Ben Fritz

unread,
Apr 5, 2012, 11:30:00 AM4/5/12
to vim...@googlegroups.com, hilal Adam

On Wednesday, April 4, 2012 1:27:21 PM UTC-5, hilal Adam wrote:
> Ben, Thanks for your prompt response.
> Somehow I am getting 3 copies of your premature, and complete replies.
>

I only see one post. Weird. I'm starting to really hate the Google
Groups interface. I may try subscribing to get emails and just filter
them all away from my inbox. Ugh.

Maybe you're getting multiple copies because you're on the CC list as
well as subscribed to the mailing list? I've never had problems with
that, but I've been using the web interface for quite some time now
without getting any email I don't subscribe to directly.

> Yes. You're right \*\* was one of the approaches I used.
> Unfortunately * is treated as a metacharacter/special character/keyword
> by grep/vimgrep/c language and this where the difficulty is originating
> from.
>

* is not special anymore if escaped with a \.

> I tried your suggestion, \*\@<!\*\*\*\@!, and I get a no pattern
> found reply while I stirring at a line that has **.
>
> How would you do this in grep on cygwin command line?
>

I don't know about using grep from the cygwin command line. The pattern
I gave uses Vim regular expressions, so you'd need to use a :vimgrep
command to use it.

Here's the breakdown:

\*\@<! - match only where there is not a literal '*' just before
\*\* - two literal '*' characters
\*\@! - match only where there is not a literal '*' as the next
character after the match

These are both zero-width matches, meaning they are not part of the
match itself. I don't think grep from the cygwin command line supports
any such concept.

Someone else (Google Groups mangled the non-ascii characers in the name)
suggested \zs and \ze which similarly won't be supported. But if you are
using an external grep command this probably won't matter, since you can
accept matches of the character (or beginning/end of line) immediately
before/after the **. The suggested \(^\|[^*]\)\*\*\([^*]\|$\) pattern
may work in grep, possibly with some tweaking for syntax.

hilal Adam

unread,
Apr 20, 2012, 1:05:15 AM4/20/12
to v...@vim.org
I an not sure if this an acceptable request or not. My apologies if not.
Would experts like Ben Fritz, Marcin, Jurgen share their vimrc files with this forum?

HA

Phil Dobbin

unread,
Apr 20, 2012, 2:26:48 AM4/20/12
to vim...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If you go to GitHub & enter vimrc as a search term, you'll get quite a
lot of results.

Not sure how many of the regular contributors to this list commit/backup
their dotfiles and/or $VIMRUNTIME directories on GitHub but a lot of
well known Vimmers (Steve Losh, Tim Pope to name but two) do & they are
most instructive in their content.

Cheers,

Phil...

- --
But masters, remember that I am an ass.
Though it be not written down,
yet forget not that I am an ass.

Wm. Shakespeare - Much Ado About Nothing


-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: §auto-key-locate cert pka ldap hkp://keys.gnupg.net
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPkQGkAAoJEKpMeDHWT5ADZk8IAKVxOkyX4pw6ZdjNInrPL9gS
rpaatAPordKVSPDJr1jNpAGpl6lOa3kbgrVcrMmnp+NynaaWbqiolXHo0ZMAte+J
UdRlaqE854/rhhrdmkKnnLT7j2LZ/a8gOletX+8rfwAtL6/ieh+lK1rL9VGu4x5u
/4xT/OuPJtOCMKCaWpQQMH2xi8YZ2FuSoEATbNdscieglwWBhiP+H2Kz00p0NdrT
tM5vLXlSAKlaLpWAoakK8fg4obOD2mWgORvh6RGxcMJaEBnevKYpBD0P6NP6Yq0+
HAH3ImL4OwpTZvDEi8cMG7PTaFnMsP9c24AbVCr3dIJmksRkenPLOLCC+gqVx9E=
=H+La
-----END PGP SIGNATURE-----

John Beckett

unread,
Apr 20, 2012, 2:44:58 AM4/20/12
to vim...@googlegroups.com
hilal Adam wrote:
> Would experts like Ben Fritz, Marcin, Jurgen share their
> vimrc files with this forum?

Google finds lots of them, but many of those are burdened with
baggage.

There are two simple samples in these tips:

http://vim.wikia.com/wiki/Example_vimrc
http://vim.wikia.com/wiki/Build_Vim_in_Windows_with_Visual_Studio

John

hilal Adam

unread,
May 17, 2012, 11:39:39 PM5/17/12
to v...@vim.org

I am bias towards VIM when it comes to editors for doing development work (mainly C). But I have this problem regarding cross referencing. I use ctags which only provides half of the equation when I search for definitions of functions and variables. However, I haven't been able to find a useful way for doing cross referencing. For example in cases where I want find where a function is used, etc. I have come across cscope which is outdated and lacks proper documentation for how to install and run. Could anyone help with pointing to the right direction.
I appreciate any help in this respect.

Gary Johnson

unread,
May 18, 2012, 1:40:49 AM5/18/12
to vim...@googlegroups.com
I don't understand how cscope "lacks proper documentation". From
within Vim there is ":help cscope" and from the shell, "man cscope".
A quick search of the web finds these:

http://cscope.sourceforge.net/cscope_vim_tutorial.html
http://www.vim.org/scripts/script.php?script_id=51
http://vim.wikia.com/wiki/Cscope
http://hthreads.csce.uark.edu/wiki/Navigating_with_Cscope

It is almost as easy to use as ctags. At the top-level of a source
tree, execute "cscope -R -b". From within Vim, execute ":cs add
cscope.out".

Granted, I use a plugin developed by a coworker and myself to allow
cscope's search features to be accessed by simple mappings, but the
references above also describe some plugins and mappings you will
probably find useful.

I couldn't live without cscope. I work on huge embedded systems
written in C and I seldom have to think about where things are
located--I just search for symbols using cscope and Vim's quickfix
list.

Regards,
Gary

JT Mitchum

unread,
May 18, 2012, 4:05:00 AM5/18/12
to vim...@googlegroups.com
Have you tried TagBar plugin?

I don't know enough to verify if it solves your problem - other than I know it uses exuberant ctags and seems to autoload them. 

I'll be watching this for a better answer - I don't think my idea really does it. 

JT
On May 17, 2012, at 10:39 PM, hilal Adam wrote:


I am bias towards VIM when it comes to editors for doing development work (mainly C). But I have this problem regarding cross referencing. I use ctags which only provides half of the equation when I search for definitions of functions and variables. However, I haven't been able to find a useful way for doing cross referencing. For example in cases where I want find where a function is used, etc. I have come across cscope which is outdated and lacks proper documentation for how to install and run. Could anyone help with pointing to the right direction.
I appreciate any help in this respect.


Ben Fritz

unread,
May 18, 2012, 11:48:54 AM5/18/12
to vim...@googlegroups.com, v...@vim.org, hilal Adam
I used to use CScope a lot. I did not find it hard to use or understand in
the slightest. I found Vim's built-in documentation (:help cscope) to be all
I really needed, plus web searches for scope for setting up the databases in
the first place. With cscope, I also used the CCTree plugin to view an
entire call tree for any given function:

http://www.vim.org/scripts/script.php?script_id=2368

I've since mostly stopped using CScope, because I've started using Eclim.
Eclim integrates Vim with Eclipse allowing you to use Eclipse's extensive
code navigation and information features, including:

• search for all references (e.g. function calls) for the item under the
cursor, similar to :cscope find s
• seacrh for the declaration of an item, similar to :cscope find g
• search for the definition of an item :cscope find g
• show the entire call tree leading to a function, similar to CCTree +
cscope.
• start a vimgrep relative to the project root, similar to :cscope find t or
:cscope find e
• fuzzy find of files within the project, similar to :cscope find f

It can do much more (like insert-mode completion) but the above is all I
used cscope for, and Eclim does it better.

http://eclim.org/features.html

Note that there is not (that I have found) an equivalent to :cscope find d
or :cscope find i, but I used these very rarely, if at all. Nor does Eclim
currently support call trees of functions *called by* the given function,
although Eclipse does, so that may come in the future.

hilal Adam

unread,
May 24, 2012, 11:59:06 PM5/24/12
to Ben Fritz, vim...@googlegroups.com, v...@vim.org
Sorry I bring this up again.
My difficulty is with installing cscope and getting it to run.

Per the documentation, I have added this line into cscope_maps.vim:
 if filereadable("cscope.out")
        cs add cscope.out 
...

and to _vimrc, I added:

if has('cscope')
" set cscopetag cscopeverbose
...

then, every time I attempt to open a file in Vim, I get following error message: 

"Error detected while processing C:\Program Files\Vim\vim73\plugin\cscope_maps.vim:
line   42:
E262: error reading cscope connection 0"

I looked for a solution with no luck.

Anyone knows how to fix this and better yet how to get cscope in Vim running?

Thanks for any help.


Sent: Friday, May 18, 2012 10:48 AM
Subject: Re: Cross referencing in VIM

Tony Mechelynck

unread,
May 25, 2012, 1:49:26 AM5/25/12
to vim...@googlegroups.com, hilal Adam, Ben Fritz, v...@vim.org
On 25/05/12 05:59, hilal Adam wrote:
> Sorry I bring this up again.
> My difficulty is with installing cscope and getting it to run.
>
> Per the documentation, I have added this line into cscope_maps.vim:
> if filereadable("cscope.out")
> cs add cscope.out
> ...
>
> and to _vimrc, I added:
>
> if has('cscope')
> " set cscopetag cscopeverbose
> ...
>
> then, every time I attempt to open a file in Vim, I get following error
> message:
>
> "Error detected while processing C:\Program
> Files\Vim\vim73\plugin\cscope_maps.vim:
> line 42:
> E262: error reading cscope connection 0"
>
> I looked for a solution with no luck.
>
> Anyone knows how to fix this and better yet how to get cscope in Vim
> running?
>
> Thanks for any help.
>

1. You need to know what directory was current when creating cscope.out.
If different from the current directory, it must be given as an
additional argument to :cscope add

2. cscope.out may have become out of date (referencing source files or
include files which don't exist anymore) and in that case you would need
to generate it again.

3. The :cscope command is only available in Vim executables compiled
with + cscope; I recommend to wrap your cscope commands in :if has('cscope')


Best regards,
Tony.
--
"If dolphins are so smart, why did Flipper work for television?"

Edward

unread,
May 26, 2012, 5:42:52 AM5/26/12
to vim...@googlegroups.com, hilal Adam, v...@vim.org
Hi Adam,
I'm using VIM to develop my projects every day. They are mainly C code with some perl/python/batch and makefiles. You didn't mention which OS you are using. So i just  want to share my experience on Windows.
cscope is a great plugin to do what you want even though it isn't updated for  a while.
You can just download it and put it into PTAH. Then use it to generate a database file. In my case, i wrote a batch file to do this :
del cscope.out
ctags -R --c++-types=+p --fields=+iaS --extra=+q
gfind . -regex ".*\.\(c\|h\|mak\|py\|pl\|bat\|cmd\)" -not -regex ".*\(no_use\).*" -type f -printf "%%p\n" >cscope.file
cscope -Rb -icscope.file
After you get cscope.out(database file), you can open the gvim and use <:cs add cscope.out> to load your database file. Then you can lookup your symbols use cs find x xxxxxxx(you can find help by simply type :cs command)

BTW: there is a great plugin named cscope_maps.vim, it provide bunches of key map for cscope usage.

锟斤拷 2012/5/18 11:39, hilal Adam 写锟斤拷:

I am bias towards VIM when it comes to editors for doing development work (mainly C). But I have this problem regarding cross referencing. I use ctags which only provides half of the equation when I search for definitions of functions and variables. However, I haven't been able to find a useful way for doing cross referencing. For example in cases where I want find where a function is used, etc. I have come across cscope which is outdated and lacks proper documentation for how to install and run. Could anyone help with pointing to the right direction.
I appreciate any help in this respect.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php


-- 
Thanks & Best Regards
Edward Xu

hilal Adam

unread,
Nov 14, 2012, 6:42:14 PM11/14/12
to v...@vim.org
I wasn't able to find a related fix for this problem. Sorry if this is a repeat question.
Hope to get some help with this.
I suddenly (not sure when) lost the syntax highlighting for my file types when I start VIM. When I attempt to do "syntax on", I get following error:
"Error detected while processing c:\Program Files\Vim\vim73\syntax\syntax.vim:
line 42:
E216: No such groug or event: filetypedetect BufRead

Thanks in advance for your help.

-H

Tony Mechelynck

unread,
Nov 14, 2012, 7:00:50 PM11/14/12
to vim...@googlegroups.com, hilal Adam
Do you have a vimrc (which Vim would see as $HOME/_vimrc on Windows or
$HOME/.vimrc on Unix-like systems)? If you don't, I recommend the
following one-liner:

runtime vimrc_example.vim

Later you may want to add more customizations, usually below that line.


Best regards,
Tony.
--
"I don't like spinach, and I'm glad I don't, because if I liked it I'd
eat it, and I just hate it."
-- Clarence Darrow


hilal Adam

unread,
Nov 14, 2012, 8:03:51 PM11/14/12
to vim...@googlegroups.com
 


From: Tony Mechelynck <antoine.m...@gmail.com>
To: vim...@googlegroups.com
Cc: hilal Adam <hil...@yahoo.com>
Sent: Wednesday, November 14, 2012 6:00 PM
Subject: Re: Syntax highlighting
-- You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Oh. Yes I do. Following lines are included:

filetype on
filetype plugin on
filetype indent on
syntax on

Thanks for your reply.

Tony Mechelynck

unread,
Nov 14, 2012, 8:32:50 PM11/14/12
to vim...@googlegroups.com, hilal Adam
On 15/11/12 02:03, hilal Adam wrote:
>
> ------------------------------------------------------------------------
> *From:* Tony Mechelynck <antoine.m...@gmail.com>
> *To:* vim...@googlegroups.com
> *Cc:* hilal Adam <hil...@yahoo.com>
> *Sent:* Wednesday, November 14, 2012 6:00 PM
> *Subject:* Re: Syntax highlighting
Hm. Then the output of the :scriptnames command should show the
following near the top:

_vimrc
filetype.vim
ftplugin.vim
indent.vim
syntax.vim
synload.vim
syncolor.vim

not necessarily in that order, and with possibly in gvim a menu.vim
somewhere between them. Full paths are included. Do they look
reasonable? I would expect these files to be in "C:/Program
Files/Vim/vim73/" and its subdirectories, shown with either forward
slashes or backslashes as separators, except for your vimrc, which
should be found in some directory which depends on your login name.


Best regards,
Tony.
--
ARTHUR: Go on, Bors, chop its head off.
BORS: Right. Silly little bleeder. One rabbit stew coming up.
"Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

Charles E Campbell

unread,
Nov 14, 2012, 10:29:48 PM11/14/12
to vim...@googlegroups.com
Tony Mechelynck wrote:
> On 15/11/12 02:03, hilal Adam wrote:
>> <snip>
>>
>> Oh. Yes I do. Following lines are included:
>>
>> filetype on
>> filetype plugin on
>> filetype indent on
>> syntax on
>>
>> Thanks for your reply.
>
<snip>

Hilal -- do you happen to have

set nocp

in your .vimrc?

Regards,
Charles Campbell

Tony Mechelynck

unread,
Nov 14, 2012, 11:01:20 PM11/14/12
to vim...@googlegroups.com, Charles E Campbell
That should make no difference, unless that vimrc is named exrc: if a
vimrc is (looked for and) found, Vim sets 'nocompatible' just before
sourcing it.

It might also make a difference if -u is used, but IMHO it doesn't make
sense to use -u %HOME%\_vimrc


Best regards,
Tony.
--
The faster we go, the rounder we get.
-- The Grateful Dead

Marcin Szamotulski

unread,
Nov 15, 2012, 5:14:47 AM11/15/12
to vim...@googlegroups.com
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php

Wow, you mean me?
- yes it is already on the net:

http://atp-vim.sourceforge.net/help.shtml

(at the bottom)


Best,
- Marcin

hilal Adam

unread,
Nov 15, 2012, 6:45:16 PM11/15/12
to vim...@googlegroups.com



From: Charles E Campbell <drc...@campbellfamily.biz>
To: vim...@googlegroups.com
Sent: Wednesday, November 14, 2012 9:29 PM
Subject: Re: Syntax highlighting
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Hi Charles,

Sorry I couldn't respond earlier. Our IT blocks all personal email access at work.
Yes I do have "set nocompatible" in my _vimrc. 
I still have the problem hope someone will be able to identify my problem.

-Hilal




Reply all
Reply to author
Forward
0 new messages