I found a nice feature of VIM search, i.e. listing all search results and the corresponding line numbers.
For example:
:g/for.*bar/#
Question: Is there an "easy" way to pipe/put this into another window/tab/buffer?
Cheers!
I don't know how to pipe the output of :g/for.*bar/#
into a new buffer but I do know how to use vimgrep
to get much the same result.
Try:
vimgrep "for.*bar" %
:copen
Now you have a buffer with all the search results and you can even navigate between them with :cn
and :cp
.
Take a look at :help quickfix
I don't know how to redirect the output directly to a buffer, but you can use redir
to send it to a register, and then paste that register to a new buffer.
:redir @a
:g/for.*bar/#
:redir END
:enew
:put! a
The #
(after :g
) prepends the line number of each result.
You could also send it to a file.
:redir > file
:g/for.*bar/#
:redir END
:e file
See :help :redir
for more.
Complimenting the accepted answer: I did a mapping to open the last search directly on the quickfix window with \ss (show search), add it to your .vimrc:
" show last search results on the quickfix window at windows bottom
nnoremap <silent><leader>ss :vimgrep /<C-r>\// % <CR> :botright copen <CR>
BR
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