Number8
Number8

Reputation: 12910

Search in buffer and display 'context' lines

:vimgrep [search text] %  

and copen give a nice listing of all [search text] in the buffer.
Is there a way to include in the results the line before and the line after each line that has a match? I'm looking in to folding to achieve this, but haven't figured that out, yet...

Upvotes: 5

Views: 1387

Answers (5)

Micah Lindstrom
Micah Lindstrom

Reputation: 365

No need for an external grep, you can print two lines of context with :cdo and :nu[mber] or :p[rint] like this:

:vimgrep "search pattern" %|cdo -2,+2print

If the result is at the start or end of the file you will see E16: Invalid range: -2,+2print. To ignore invalid lines and print whatever's available:

:vimgrep "search pattern" *|cdo execute max([1, (line('.') - 2)]) . ',' . min([line('$'), (line('.') + 2)]) 'print'

Additional reference info

Vimgrep multiline match

vimgrep multiline match is simple with

  • .* to pick up entire line
  • \n for newline characters
  • ()? to make the newline optional in case it's at start or end of file.

With escaping the full command is:

:vimgrep "\(.*\n\)\?.*search pattern.*\(\n.*\)\?" *

However the quickfix list will not display multi-line entries, so that's a dead end.

Upvotes: 0

hulius
hulius

Reputation: 359

To display your search with context in a separate window, you can do:

:grep -C 1 MYSEARCH %   # search with 1 line of context
:cw                     # open the quickfix window

You can use :silent grep [...] if you don't want to see the first window opened by :grep.

You won't have folding but otherwise it'll do the job!

Upvotes: 0

anon
anon

Reputation:

The foldutil plugin can fold the buffer, leaving only the areas matching the search pattern unfolded.

Generally, it's quite flexible, capable of folding things in various ways. Your particular use case would work like this:

:FoldNonMatching <pattern> <number-of-context-lines>

For instance,

:FoldNonMatching \<function\> 3

Be sure to read the documentation to understand all the plugin can offer.

Upvotes: 0

doliver
doliver

Reputation: 1030

Here's something similar: :g/[search text]/-1,+1nu which will show you context above and below the [search text] along with the line number for that line. You can quickly jump there afterward using :lnum where lnum is the line number. In general, I wouldn't do this - I would instead just press enter on the quickfix list to jump around and look at the results.

Upvotes: 4

Ingo Karkat
Ingo Karkat

Reputation: 172788

In order to appear in the quickfix window, :vimgrep would need to match the context lines, too. Building that "match above and below" into the search regular expression would be difficult, and you'd still have no folding.

If you use :grep instead of :vimgrep, you can specify a context (-C NUM), but that wouldn't fold automatically, neither.

Another option would be to postprocess the quickfix results (via :autocmd QuickFixCmdPost), but getting and folding those lines isn't trivial.

The closest solution I've seen so far is the fold expression described on the Folding with Regular Expression Vim Tips Wiki page. This is for the current buffer only, but it's quick to set up and enables the (one or two level) context folding.

Upvotes: 2

Related Questions