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?
From the root of your project, you can search through all your files recursively from the current directory like so: grep -R '. ad' . The -R flag is telling grep to search recursively.
Unlike normal within-file search, vimgrep (or rather, the quickfix list) tells you which match you're on (e.g. 2 of 5 ). If you want to search within the current file but have this feature, tell vimgrep to search the current file with % : :vimgrep /foo/g %
vi has a total of 27 buffers where you can store text. There is one for each letter of the alphabet, and an `unnamed' buffer, which is where any text you delete goes. Any deleted text pushes the contents of the unnamed buffer into buffer `1'. Anything that was in buffer `1' goes into buffer `2', etc.
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"
:grep
& co. will populate the QuickFix buffer, which allows for fast navigation among 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?"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With