10

With many buffers open, I need a simple way to search all buffers for a regex and navigate the search result (quick list?)

I know I can :bufdo command, and it is easy to search and replace with %s, but I can't find a way to do just a simple search and then navigate the results.

I found plugins for that (e.g., buffergrep), but I'll be surprised if this simple task is not natively supported with a vim trick.. is it?

Samer Buna
  • 8,821
  • 9
  • 38
  • 55

3 Answers3

4

:grep & co. will populate the QuickFix buffer, which allows for fast navigation among results.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 2
    Can you use that to grep in BUFFERS and not files – Samer Buna Dec 08 '10 at 20:09
  • 3
    @Samer Abukhait: The documentation suggests `:call setqflist([]) | bufdo grepadd! something %`, with some additional work to handle more cases. – ephemient Dec 08 '10 at 23:41
  • grepadd! seems to do the trick. The documentation isn't clear enough for me to map it to the desired behavior or answer questions like (what are the syntax elements? and/or can I use something else other than "%"?). But in all cases, I now have a way for doing what I wanted, and for that I thank you! – Samer Buna Dec 15 '10 at 02:45
  • As for buffers which are already loaded, `vimgrep` instead of `grep` is a better solution. – Bohr Jan 01 '14 at 03:39
4

from :help grepadd

:grepa[dd][!] [arguments]
            Just like ":grep", but instead of making a new list of
            errors the matches are appended to the current list.
            Example:
                :call setqflist([])
                :bufdo grepadd! something %
            The first command makes a new error list which is
            empty.  The second command executes "grepadd" for each
            listed buffer.  Note the use of ! to avoid that
            ":grepadd" jumps to the first error, which is not
            allowed with |:bufdo|.
            An example that uses the argument list and avoids
            errors for files without matches:
                                :silent argdo try 
                  \ | grepadd! something %
                  \ | catch /E480:/
                  \ | endtry"
eolo999
  • 863
  • 6
  • 13
2

"I found plugins for that (e.g., buffergrep), but I'll be surprised if this simple task is not natively supported with a vim trick.. is it?"

Not that I know of. And existence of multiple plugins trying to offer this functionality tends to confirm that. . .

What plugins have you tried and what have they been lacking?

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

Also, just to make sure, you are aware of vimgrep, right? Vimgrep is an internal command that loads files into buffers and does greps on the buffers, with results in quickfix window. I haven't confirmed, but I assume if a searched file is already open in a buffer that Vimgrep doesn't reload it, at least not if it has 'nomodified' flag set. If so, one way to use Vimgrep for quick-and-easy buffer grepping would be to just create a file list for Vimgrep using the output from the :buffers command.

Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54