Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe search result to other tab/window/buffer in VIM

Tags:

vim

search

pipe

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!

like image 350
ezdazuzena Avatar asked Nov 09 '12 10:11

ezdazuzena


3 Answers

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

like image 54
Benj Avatar answered Nov 03 '22 22:11

Benj


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.

like image 33
Thor Avatar answered Nov 03 '22 20:11

Thor


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

like image 1
MaikoID Avatar answered Nov 03 '22 21:11

MaikoID