Sunday, April 3, 2011

Re: basic help with grep in vim 7.3

On 2011-04-03, Daniel M. Eldridge wrote:
> I'm bamboozled about vim's internal grep.
>
> I'm looking for all files that contain the pattern "pattern" in the current
> directory (is there a way to do a recursive search?) so I type:
> :vimgrep!/pattern/gj *.*
>
> and vim says:
> (1 of 4) Then vim shows me the line that contains the pattern
> though I can't figure out what the gj does without it I only see the first file
>
> How do I move on to the second, third, and fourth files?

Vim's internal grep, vimgrep, is one of a number of Vim commands
that create lists of file names and line numbers in what are
referred to as quickfix lists. So, to move on to the second, third
and fourth files you need to learn to navigate quickfix lists. That
is covered in

:help quickfix.txt

The following commands are the ones probably used most often and
will get you started.

:cn jump to the next match
:cp jump to the previous match
:cnf jump to the next file
:cpf jump to the previous file

That's a lot to type just to jump back and forth among matches,
though, so I use these mappings:

:nmap <C-N> :cn<CR>
:nmap <C-P> :cp<CR>

You can use vimgrep to perform recursive searches. This is
demonstrated at the bottom of

:help :vimgrep

and explained in the link at the very bottom of that section, which
points to

:help starstar-wildcard

So to perform your search recursively, you'd execute

:vimgrep!/pattern/gj **/*.*

The section ":help :vimgrep" also explains the g and j flags. I
think the explanation of the j flag there is pretty straightforward,
but to be honest, it's not clear to me from that explanation what
the effect of the g flag is. I don't use :vimgrep very much.

HTH,
Gary

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

No comments: