4

One thing that I like to do from time to time is do a quick search on the directory I am working on and find all the references to a specific string, specially when debugging code from some one else. Is it still not possible for VIM to do such search? Is there an alternative to do it directly with plain terminal?

ATM (since I'm still learning VIM) I do the search in TextMate and find the string that way.

Helmut Granda
  • 4,565
  • 7
  • 35
  • 51
  • I just found this plugin that dates back to 2007? Anything newer or is it stable enough to use? http://www.vim.org/scripts/script.php?script_id=1813 – Helmut Granda Jan 08 '12 at 03:35
  • 1
    See the following very similar questions: "[Loading a set of files obtained via command into Vim buffers](http://stackoverflow.com/q/6870139/254635)", "[Efficient way to refactor a class/method/string within a directory using vim](http://stackoverflow.com/q/7203963/254635)", "[Vim: How can I search a word in whole project/folder recursively?](http://stackoverflow.com/q/7950558/254635)". – ib. Jan 08 '12 at 06:03

4 Answers4

7

You can use the vim command :vimgrep. This will search for a pattern in the specified files and puts the results in the quickfix window which you can then use to jump to a specific match. So for example to search for foo recursively in every .php file in your current directory you would do

:vimgrep "foo" **/*.php

Another option is the command :grep which actually calls the grep program (or whatever grepprg is set to). It also puts the results in the quickfix window for easy navigation. However this requires that you have grep on your system.

David Brown
  • 13,336
  • 4
  • 38
  • 55
4

vim's an editor, not really a file searcher. It's trivially simple to call out to a shell and run 'grep', however. Assuming you're on a Unix-ish environment (TextMate - MacOs?) While in command mode, hit ctrl-z and you'll be at the shell prompt, then do

grep somestring *.c

and you'll get all matches for 'somestring' in any C source files.

Once done grepping, just do a fg (foreground) command and boom - back to VIM.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    Nice idea, I like your recommendation and it works well. Even better when I can do grep somestring * . * and it returns all instances next to file names. – Helmut Granda Jan 09 '12 at 05:00
1

vimgrep will work, but if the source happens to be in Git then you can use tpope's excellent https://github.com/tpope/vim-fugitive plugin, which exposes :Ggrep which hangs off git grep for more flexibility.

If it's specific entities you're looking for (functions, variables, etc) the integration with ctags is probably of interest to you as well.

richo
  • 8,717
  • 3
  • 29
  • 47
0

Sounds like you might like to have a look at vim tag search functionality combined with ctags. ctags is an utility that can be used to generate an index (a tags file) of various language objects for source code (full project tree, not just a directory). In vim a tag is sort of identifier that can be jumped to or searched for.

See vim documentation:

     :help tagsrch

Or:

http://vimdoc.sourceforge.net/htmldoc/tagsrch.html#ctags

klaus
  • 359
  • 1
  • 3