vimgrep a word that is under the cursor

2,413 views
Skip to first unread message

Steven Woody

unread,
Sep 19, 2009, 7:22:10 AM9/19/09
to Vim
Hi,

If what I want to search by vimgrep is a word that is currently under the cursor.  How could I save myself from typing again the word again?

Thanks.

--
Life is the only flaw in an otherwise perfect nonexistence
   -- Schopenhauer

narke
public key at http://subkeys.pgp.net:11371 (narke...@gmail.com)

John Beckett

unread,
Sep 19, 2009, 7:44:35 AM9/19/09
to vim...@googlegroups.com
Steven Woody wrote:
> If what I want to search by vimgrep is a word that is
> currently under the cursor. How could I save myself from
> typing again the word again?

That point is covered here:
http://vim.wikia.com/wiki/Find_in_files_within_Vim

John

Steven Woody

unread,
Sep 19, 2009, 12:43:28 PM9/19/09
to vim...@googlegroups.com
I see that is <cword>, but while I can do ':vimgrep /\<my_word\>/ **/*.c, but the ':vimgrep /\<<cword>\>/ **.c' failed, it just found nothing.  What's wrong with my typing? Thanks.

Gary Johnson

unread,
Sep 19, 2009, 1:41:40 PM9/19/09
to vim...@googlegroups.com
On 2009-09-19, Steven Woody wrote:
> Hi,
>
> If what I want to search by vimgrep is a word that is currently under the
> cursor. How could I save myself from typing again the word again?

After you've typed ":vimgrep /", type Ctrl-R Ctrl-W to insert the
word under the cursor. See

:help c_CTRL-R_CTRL-W

Regards,
Gary


Steven Woody

unread,
Sep 19, 2009, 2:59:42 PM9/19/09
to vim...@googlegroups.com
Thanks, this is good!

John Beckett

unread,
Sep 19, 2009, 8:47:00 PM9/19/09
to vim...@googlegroups.com
Steven Woody wrote:
>> That point is covered here:
>> http://vim.wikia.com/wiki/Find_in_files_within_Vim
>
> I see that is <cword>, but while I can do ':vimgrep
> /\<my_word\>/ **/*.c, but the ':vimgrep /\<<cword>\>/ **.c'
> failed, it just found nothing.

I was too lazy to tell you about Ctrl-R Ctrl-W.

If you want to search all *.c files in the current directory,
you would execute the following (put cursor on line, press Y
then type @" or just put it in your vimrc):

:map <F4> :execute "vimgrep /" . expand("<cword>") . "/j *.c"<Bar>cw<CR>

Now put the cursor on any word and press F4.

John

Steven Woody

unread,
Sep 19, 2009, 11:09:03 PM9/19/09
to vim...@googlegroups.com
Thanks for the mapping.  The only thing I don't understand is the "<Bar>cw", what does it mean and for?  Thanks.

 
Now put the cursor on any word and press F4.

John



John Beckett

unread,
Sep 20, 2009, 1:08:48 AM9/20/09
to vim...@googlegroups.com
Steven Woody wrote:
>> :map <F4> :execute "vimgrep /" . expand("<cword>") .
>> "/j *.c"<Bar>cw<CR>
>
> Thanks for the mapping. The only thing I don't understand is
> the "<Bar>cw", what does it mean and for? Thanks.

The <Bar> means '|' and it delimits another command.
That command is :cw (:help :cwindow) which displays the results.
The 'j' in '/j' means you do NOT jump to the first hit.

John

Steven Woody

unread,
Sep 20, 2009, 5:25:25 AM9/20/09
to vim...@googlegroups.com
Thanks,  I just thought I've met 'cw' somewhere, now it's clear.
 

Reply all
Reply to author
Forward
0 new messages