how grep results into separate buffer or split window


how grep results into separate buffer or split window



i try to grep current file and then the results to be in separate buffer or split window , NOT in the current buffer im in
i need it to work with simple vim not using quickfix methods (vimgrep and such)
i did try this :


:r!grep foo %



VIM: How to store the grep results in a buffer



but it replaces the current buffer and only doing undo returns the previous file content
so my question is:
how to redirect the results or separate buffer or split window




2 Answers
2



Open a new buffer (here in a vertical window):


:vnew



Fill it with the output of a grep of the alternate file:


grep


:0r!grep foo #



In one go:


:vnew | 0r!grep foo #





Huh, I would have sworn it wouldn't let me put the contents of grep into an unnamed buffer.
– Christian Gibbons
2 days ago


grep





And you would be wrong to swear that :-)
– romainl
2 days ago





Quite. Sure makes my solution look like the vim equivalent of the ioccc.
– Christian Gibbons
2 days ago



I'm not sure if this works perfectly in all scenarios, but give this a shot:



:execute "vnew" "tmp_buffer_" . winbufnr(winnr()) | execute "0r !grep foo" bufname(winbufnr(winnr()+1))


:execute "vnew" "tmp_buffer_" . winbufnr(winnr()) | execute "0r !grep foo" bufname(winbufnr(winnr()+1))



The first part should create a new buffer in a vertical split. I gave it a subscript with the buffer number it is associated with to avoid possible clashes with running this on multiple buffers. The second performs the read operation and figures which buffer to run the grep on by assuming the buffer to be grepped is in the next window over (which I believe should hold true since we've done a fresh split)





Use expand() with the alternative buffer is simpler. e.g. command! -nargs=+ Bgrep new | pu=system('grep <args> ' . expand('#')) | 0d_. use like :Bgrep foo
– Peter Rincker
2 days ago


expand()


command! -nargs=+ Bgrep new | pu=system('grep <args> ' . expand('#')) | 0d_


:Bgrep foo





@PeterRincker forgive my yellow-belt level vimfu, but how does the alternate file come into play? Isn't that for buffers sharing the same window?
– Christian Gibbons
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV