Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store grep results in a buffer in Vim?

Tags:

vim

I want to debug a process, hence I attached strace to the process and redirected the output to a file and then performed the operation. During the process it has created a lot of processes. So here is what I want to do, I want to select all the system calls executed by a process. To do that I used grep command with pattern as pid:

:grep pid %

It shows the result but I am not able to traverse through the result, it promts

Press ENTER or type command to continue

and returns to the file. What I would like to do is store the result in a buffer and then have a look into it and if required save it into a file or else discard it and return to the original file. Is there a way to do this with out exiting from the vim editor? Thanks in advance.

I would like to search with the result and store that in a buffer.

like image 412
hue Avatar asked Jun 16 '11 14:06

hue


People also ask

Can we use grep with vi editor?

If you want the output to go into a file, use grep ... > /tmp/foo . You can add && vi /tmp/foo on the end if you want to edit that file immediately. In fact there's no such thing as a "vi file". vi operates on arbitrary text files; the files themselves are not directly associated with vi .

How do I grep in Vim?

You simply type :Grep foobar , and it will search in your current directory through all file extensions (except json and pyc; you can add more to the blacklist). It also displays the results in a nice little buffer window, which you can navigate through with normal HJKL keys, and open matches in the main editor window.


2 Answers

Over the years of reading all kind of logs, I learned this little trick:

:%!grep pattern

It simply replaces the current buffer contents with grep output (so to go back to the original logs you have to simply press u).

You can also use it with other tools:

:%!ack pattern
:%!ag pattern
:%!rg pattern

Note that you can also run these commands on other files then the current one. The following 2 commands would replace the current buffer with results of grepping over the current file (second % character, which would be redundant for grep in this case) and otherfile.txt respectively:

:%!grep pattern %
:%!grep pattern otherfile.txt

For me it's the simplest and the best solution for fast grepping of big files in Vim and I'm pretty surprised no one ever mentioned it.

like image 180
Michał Góral Avatar answered Oct 01 '22 07:10

Michał Góral


You can go to older searches, and back easily:

:copen
:colder " goes to older
:cnewer " newer

You can have another search using lvimgrep (uses location window):

:lopen
:lnext
etc...

It also has history:

:lolder
:lnewer

You can read into any buffer:

:r!grep bla **/*.cs

Finally, any command that gives output, can be redirected with the redir command:

:redir >> file
:grep bla **/*.cs
:redir END

See :he redir for the many ways to use it (redirect into registers or variables).

like image 27
sehe Avatar answered Oct 01 '22 06:10

sehe