Fold the output of grep

89 views
Skip to first unread message

howard Schwartz

unread,
Jun 29, 2011, 10:46:42 PM6/29/11
to vim...@googlegroups.com
Here's what I would like to do: Using gnu grep (or similar one), search files
for some string, STRING, with the output including a context line before and
after each line found. That would look like:

filename:21: This is a context line
filename:22: This line contains STRING
filename:23: This is another context line

I save this output in a file and then open the file with vim. I want vim
to fold the context lines, so I see only lines containing STRING. Now I can
look at the target lines to find the one I am interested in, and if I need
some context to help me, I can unfold the context lines. I should be able to
use gF to open any file and line I want to go to.

For that that would be the perfect grep. I want it in particular, to search
my zillions of saved emails for a message containing a pattern.

It would be nice if I could unfold both the context line before and after the
target line, with one operation. But I do not know how to do that. Is there a
way to make the visible linei of a fold, be the line in the middle of that
fold?

A quickfix list would do some of this, but I do not think such a list can
display output results that include context lines. I tried it once using gnu
grep as the external grep - but it did not seem to work.

Suggestions on how to accomplish this?

Ben Fritz

unread,
Jun 29, 2011, 11:22:32 PM6/29/11
to vim_use


On Jun 29, 9:46 pm, howard Schwartz <howard...@gmail.com> wrote:
> Here's what I would like to do: Using gnu grep (or similar one), search files
> for some string, STRING, with the output including a context line before and
> after each line found. That would look like:
>
> filename:21: This is a context line
> filename:22: This line contains STRING
> filename:23: This is another context line
>
> I save this output in a file and then open the file with vim. I want vim
> to fold the context lines, so I see only lines containing STRING. Now I can
> look at the target lines to find the one I am interested in, and if I need
> some context to help me, I can unfold the context lines. I should be able to
> use gF to open any file and line I want to go to.
>
> For that that would be the perfect grep. I want it in particular, to search
> my zillions of saved emails for a message containing a pattern.
>

http://vim.wikia.com/wiki/Fold_regular_expression

> It would be nice if I could unfold both the context line before and after the
> target line, with one operation. But I do not know how to do that. Is there a
> way to make the visible linei of a fold, be the line in the middle of that
> fold?
>

With the script at the above link, there are two levels of fold, with
more context at each level. You can use zr/zm to increase/decrease the
fold level, but this affects the entire buffer.

Marko Mahnič

unread,
Jun 30, 2011, 3:13:01 AM6/30/11
to vim_use
On Jun 30, 4:46 am, howard Schwartz <howard...@gmail.com> wrote:
> Here's what I would like to do: Using gnu grep (or similar one), search files
> for some string, STRING, with the output including a context line before and
> after each line found. That would look like:
>
> filename:21: This is a context line
> filename:22: This line contains STRING
> filename:23: This is another context line

The command VxOccur from the script 2606 will run grep and turn the
above list into something like:
filename
21: This is a context line
22: This line contains STRING
23: This is another context line
filename 2
...
It will display the matches in a popup window. Then you can use the
filter mode (press f) to narrow the results interactively. If you
search for 'STR' you would see:
filename
22: This line contains STRING

In the new (unpublished) version the 'STR' in STRING would also be
highlighted.

You have to set g:vxoccur_grep_mode to 1 or 2 if you want to use grep
(0 uses :vimgrep).
For grep to display N context lines, add -CN to the range of files to
search when prompted.

VxOccur documentation:
http://vimuiex.sourceforge.net/vimdoc/vimuiex_plugins.html#vimuiex-vxoccur
The script
http://www.vim.org/scripts/script.php?script_id=2606

The preview of the next version:
http://sourceforge.net/projects/vimuiex/files/tar/vim-7.3-226-puls-vimuiex-0.8.tar.bz2/download
This implements the popuplist() function in C. Different algorithms
are implemented for filtering the displayed items. They are described
in help, see :h puls-matchers, or the file runtime/doc/popuplist.txt
in the tar file.

Marko

howard Schwartz

unread,
Jun 30, 2011, 9:54:27 PM6/30/11
to vim...@googlegroups.com, Marko Mahnic
Your output sounds great, and the grep display format is one I prefer, but it
is not supported directly by gnu grep.

Is there a way to fold the context lines that appear in your popup window?
If so, how?


Marko Mahnič

unread,
Jul 1, 2011, 2:54:23 AM7/1/11
to vim_use
On Jul 1, 3:54 am, howard Schwartz <howard...@gmail.com> wrote:
> Is there a way to fold the context lines that appear in your popup window?
> If so, how?

Not at the moment. The idea is interesting, though, so I will add it
to my todo list and implement it some day.

Marko
Reply all
Reply to author
Forward
0 new messages