9

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!

ezdazuzena
  • 6,120
  • 6
  • 41
  • 71
  • Do you mean "put this list and the numbers in a new buffer"? – romainl Nov 09 '12 at 11:13
  • @romaini: indeed. For example into a split window.. something to copy from, to scroll through. Right now I get the list but when I move it "disappears". – ezdazuzena Nov 09 '12 at 11:17

4 Answers4

12

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

Benj
  • 31,668
  • 17
  • 78
  • 127
  • looks like this it what I'm looking for. Could you be so kind and explain what the '%' does? What exactly means 'copen', I mean, where does the result from vimgrep go to? – ezdazuzena Nov 09 '12 at 11:31
  • 1
    @ezdazuzena Sure `%` just get's replaced with the path to the current file in command mode. – Benj Nov 09 '12 at 11:32
  • Thanks. With respect to the second part of my question, I found the answer here: http://blogs.gurulabs.com/stuart/archives/2006/08/vimgrep-copen.html – ezdazuzena Nov 09 '12 at 11:35
  • The quickfix window can be quite handy. I would suggest making mappings for `:cnext` and `:cprev` if find yourself using the quickfix window often. I personally suggest Tim Pope's unimpaired.vim plugin ( https://github.com/tpope/vim-unimpaired ). It provides the mappings `]q`, `[q`, and a few more mappings to move though the quickfix list. – Peter Rincker Nov 09 '12 at 14:32
  • If anyone is wondering, you use `vimgrep` in command mode. Type: `:vimgrep "for.*bar" %` – liammulh Feb 24 '23 at 00:34
4

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.

henrebotha
  • 1,244
  • 2
  • 14
  • 36
Thor
  • 45,082
  • 11
  • 119
  • 130
  • 2
    ... and the register can then be `:put! a` into a new buffer. – Ingo Karkat Nov 09 '12 at 11:33
  • 1
    What is `#` at the end of the search? Also, will this include line numbers? (I'm assuming not) Do you know of any way that you can get line numbers using this method? If it were possible to do a `g/some_pattern/get_ln() the_math/p` in the same way that you can `s//p`, this would be somewhat trivial, but I cannot find any way to do so. – Christian Reall-Fluharty Jul 10 '19 at 16:03
  • 1
    @christianreallfluharty: the hash command (`#`) given to `:global` prepends line number to the match. – Thor Jul 19 '19 at 13:21
1

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

MaikoID
  • 4,359
  • 4
  • 25
  • 27
0

It's quite a bit later, but for a different approach, I did this with a mapping that uses cword and then sends the content to a buffer. For my workflow this has been the most helpful way to do it.

" Search for the word under the cursor in the entire file and take all lines and put them into a new vsplit buffer
noremap <leader>* :redir @s<CR>:silent g/^R^W/p<CR>:redir END<CR>:botright vnew<CR>:silent put s<CR>:nohlsearch<CR>
Raj
  • 1,764
  • 2
  • 13
  • 14