Vim Tips Wiki
(cleanup to see text)
No edit summary
Line 1: Line 1:
{{review}}
+
{{Duplicate|77}}
 
{{Tip
 
{{Tip
 
|id=282
 
|id=282
Line 11: Line 11:
 
}}
 
}}
   
  +
When searching with /, it would sometimes be nice to fold everything except for matches to your search. The following code does this, providing 2 levels of folding to allow you to show some context around each search match as well:
Well, I've tried to understand some of the folding scripts, but life's too short. Instead, I added the following lines to my vimrc file.
 
set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2
 
map \z :set foldmethod=expr foldlevel=0 foldcolumn=2<CR>
 
   
  +
<pre>
The first line is an extension of <code>foldexpr=(getline(v:lnum)=~@/)?0:1 </code>
 
 
set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2
 
map \z :set foldmethod=expr foldlevel=0 foldcolumn=2&lt;CR&gt;
  +
</pre>
   
 
*The first line is an extension of <pre>foldexpr=(getline(v:lnum)=~@/)?0:1</pre>
The second line (re)sets the foldmethod to expr(ession) plus.
+
*The second line (re)sets the foldmethod to expr(ession) plus.
   
 
To add a second level of context, you could add <pre>(getline(v:lnum-2)=~@/)\|\|(getline(v:lnum+2)=~@/)?2:3</pre> but it will take longer as folded lines (the majority) evaluate the full expression.
   
 
First search for /regexp/, then fold everything else with \z
 
First search for /regexp/, then fold everything else with \z
  +
Use zr to display more context, use zm to display less context.
   
  +
If no context is desired, you can do the following instead:
Use zr to reveal more context (before/after) lines.
 
  +
<pre>
 
set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum)=~@/)\|\|(getline(v:lnum)=~@/)?0:1
 
map \z :set foldmethod=expr foldlevel=0 foldcolumn=1&lt;CR&gt;
  +
</pre>
   
 
This tip combines well with [[VimTip108]] (space bar in normal mode toggles a fold).
You could add <code>(getline(v:lnum-2)=~@/)\|\|(getline(v:lnum+2)=~@/)?2:3 </code>, but it will take longer as folded lines (the majority) evaluate the full expression.
 
   
  +
This script does the same thing and more: {{script|id=158}}
 
What could be easier?
 
   
 
== Comments ==
 
== Comments ==
  +
TODO: Explain better what is going on.
This is a simple but powerful idea. If you want more control on the amount of context to show around the matching lines, try the following script: {{script|id=158}}
 
 
----
 
But why use two levels? I also like it a little cleaner, so I don't include context lines. <br/>
 
This basically folds every line that does not contain the search pattern:
 
set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum)=~@/)\|\|(getline(v:lnum)=~@/)?0:1
 
map \z :set foldmethod=expr foldlevel=0 foldcolumn=1&lt;CR&gt;
 
nnoremap &lt;silent&gt; &lt;space&gt; :exe 'silent! normal! za'.(foldlevel('.')?'':'l')&lt;cr&gt;
 
 
I also combine this with [[VimTip108]] (space bar in normal mode toggles a fold).
 
 
----
 
----
   
 
[[Category:Folding]]
 
[[Category:Folding]]
  +
[[Category:Searching]]

Revision as of 00:30, 13 October 2007

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #282 - Folding with Regular Expression

Created: July 11, 2002 20:55 Complexity: basic Author: Chris Butler (cz_butler--AT--yahoo.com) Version: 6.0 Karma: 54/23 Imported from: Tip#282

When searching with /, it would sometimes be nice to fold everything except for matches to your search. The following code does this, providing 2 levels of folding to allow you to show some context around each search match as well:

set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2 
map \z :set foldmethod=expr foldlevel=0 foldcolumn=2<CR> 
  • The first line is an extension of
    foldexpr=(getline(v:lnum)=~@/)?0:1
  • The second line (re)sets the foldmethod to expr(ession) plus.

To add a second level of context, you could add

(getline(v:lnum-2)=~@/)\|\|(getline(v:lnum+2)=~@/)?2:3

but it will take longer as folded lines (the majority) evaluate the full expression.

First search for /regexp/, then fold everything else with \z Use zr to display more context, use zm to display less context.

If no context is desired, you can do the following instead:

set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum)=~@/)\|\|(getline(v:lnum)=~@/)?0:1 
map \z :set foldmethod=expr foldlevel=0 foldcolumn=1<CR> 

This tip combines well with VimTip108 (space bar in normal mode toggles a fold).

This script does the same thing and more: script#158

Comments

TODO: Explain better what is going on.