search something from vim in certain directory

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

search something from vim in certain directory

Peng Yu
Hi,

I have a C++ project in some dir say "project" or its subdir. Although
I can use find and grep outside vim to search for any word in
"project". But this is not very convenient.

I want to do the search in vim. And vim should give me a list of all
the occurrences of the searched word. Then I can select any item from
the list to view it context.

The function that I want is somehow similar to :tselect, but they are
not the same. I'm wondering if somebody has the solution.

Thanks,
Peng
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

Jean-Rene David-2
* Peng Yu [2007.02.22 11:15]:
> I have a C++ project in some dir say "project"
> or its subdir. Although I can use find and grep
> outside vim to search for any word in "project".
> But this is not very convenient.

Well you can use an external grep from within vim.
One advantage is that it's relatively fast and you
can use the quickfix window. An inconvenient is
that you can't use vim regex.

:h grep
:h grepprg

If you have vim 7, then you can use vimgrep.

:h vimgrep

If your project is large, you could also use an
indexing program like "glimpse". See

http://www.vim.org/tips/tip.php?tip_id=1021

--
JR
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

Ryan Phillips-10
In reply to this post by Peng Yu
Peng Yu <[hidden email]> said:

> Hi,
>
> I have a C++ project in some dir say "project" or its subdir. Although
> I can use find and grep outside vim to search for any word in
> "project". But this is not very convenient.
>
> I want to do the search in vim. And vim should give me a list of all
> the occurrences of the searched word. Then I can select any item from
> the list to view it context.
>
> The function that I want is somehow similar to :tselect, but they are
> not the same. I'm wondering if somebody has the solution.
>

You may want to check out the 'tags' feature also.  :he tags.  You
will need exuberant-ctags from ctags.sf.net.

-Ryan
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

Peng Yu
In reply to this post by Jean-Rene David-2
On 2/22/07, Jean-Rene David <[hidden email]> wrote:

> * Peng Yu [2007.02.22 11:15]:
> > I have a C++ project in some dir say "project"
> > or its subdir. Although I can use find and grep
> > outside vim to search for any word in "project".
> > But this is not very convenient.
>
> Well you can use an external grep from within vim.
> One advantage is that it's relatively fast and you
> can use the quickfix window. An inconvenient is
> that you can't use vim regex.
>
> :h grep
> :h grepprg
>
> If you have vim 7, then you can use vimgrep.
>
> :h vimgrep
>
> If your project is large, you could also use an
> indexing program like "glimpse". See
>
> http://www.vim.org/tips/tip.php?tip_id=1021

Hi,

I read the help. But I still don't see how to search in all the *.h
and *.cc in a certain directory. Do I have to rely on the external
command "find"?

Thanks,
Peng
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

Jean-Rene David-2
* Peng Yu [2007.02.22 16:15]:
> I read the help. But I still don't see how to search in all the *.h
> and *.cc in a certain directory. Do I have to rely on the external
> command "find"?

It would help if you told us what version of vim
you use and what you tried.

If you have vim 7, you can use:

:vimgrep pattern directory/**/*.h

See

:h wildcard

Otherwise:

:grep -r pattern directory

though that will search all files in the
directory, recursively.

If your 'shell' is zsh, then you can use zsh's
recursive globbing patterns:

:grep pattern directory/**/*.(h|cc)(.)

If you use indexing with glimpse, you can specify
which files in which directory to index.

man glimpse
man glimpseindex

--
JR
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

A.J.Mechelynck
In reply to this post by Peng Yu
Peng Yu wrote:

> On 2/22/07, Jean-Rene David <[hidden email]> wrote:
>> * Peng Yu [2007.02.22 11:15]:
>> > I have a C++ project in some dir say "project"
>> > or its subdir. Although I can use find and grep
>> > outside vim to search for any word in "project".
>> > But this is not very convenient.
>>
>> Well you can use an external grep from within vim.
>> One advantage is that it's relatively fast and you
>> can use the quickfix window. An inconvenient is
>> that you can't use vim regex.
>>
>> :h grep
>> :h grepprg
>>
>> If you have vim 7, then you can use vimgrep.
>>
>> :h vimgrep
>>
>> If your project is large, you could also use an
>> indexing program like "glimpse". See
>>
>> http://www.vim.org/tips/tip.php?tip_id=1021
>
> Hi,
>
> I read the help. But I still don't see how to search in all the *.h
> and *.cc in a certain directory. Do I have to rely on the external
> command "find"?
>
> Thanks,
> Peng
>

To search for the word "word" (without quotes, but as an integral word) using
Vim 7:

(a) in all *.c, *.cpp and *.h files in the current directory but not below:

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

(b) the same, but recurse into subdirectories:

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

If *.[ch] doesn't work on your system, replace it by *.c *.h


Best regards,
Tony.
--
Be assured that a walk through the ocean of most Souls would scarcely
get your Feet wet.  Fall not in Love, therefore: it will stick to your
face.
                -- National Lampoon, "Deteriorata"
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

Pavel Shevaev
In reply to this post by Jean-Rene David-2
> :vimgrep pattern directory/**/*.h

Just an aside question - is there anything alike for find & replace in
files? Something like :%s/../../g <glob> could be very helpful...
Especially it would be great to see replaced stuff in quickfix window.

--
Best regards, Pavel
Reply | Threaded
Open this post in threaded view
|

Re: search something from vim in certain directory

MarcWeber
On Fri, Feb 23, 2007 at 04:26:50PM +0300, Pavel Shevaev wrote:
> >:vimgrep pattern directory/**/*.h
>
> Just an aside question - is there anything alike for find & replace in
> files? Something like :%s/../../g <glob> could be very helpful...
> Especially it would be great to see replaced stuff in quickfix window.
Not as far is I know
You can use :n path/**/* to open all files and use commands like bufdo
to do the substitution.

To check results I'd recommend cp -r dir /tmp/dir and diff -r dir
/tmp/dir .. which should give you the results.

Also this piece of code was pasted somewhere on the list month ago:

 " by Piet Delport a few years ago.
 command! DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis | wincmd p | diffthis

Then you can diff the changed files before :saveall ;)

HTH Marc