Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(→‎Uses: mention that syntax highlighting is better way to do this if available)
Line 60: Line 60:
 
command! Japi Foldsearch public\s\|protected\s\|private\s
 
command! Japi Foldsearch public\s\|protected\s\|private\s
 
</pre>
 
</pre>
  +
  +
Note that a better way to do this for languages that support it would be to use syntax highlighting. [[Check your syntax files for configurable options|Check the syntax file]] for the language in question to determine if this is an option. Many syntax files such as those for C, Perl, and [[Syntax folding of Vim scripts|VimL]] all define rules for at least some syntax-based folding, using <tt><nowiki>:set foldmethod=syntax</nowiki></tt>. Other languages such as Java do not currently have this functionality built in, so keep this idea around just in case you need it!
   
 
==See Also==
 
==See Also==

Revision as of 18:33, 21 March 2008

Tip 282 Printable Monobook Previous Next

created July 11, 2002 · complexity basic · author Chris Butler · version 6.0


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.

Using a '/' Search and Key Mapping

Add the following to a vimrc file to provide a mapping to fold on an already-performed search:

map \z :setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2 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.

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

Using a User-defined Command

If you want to search and fold with a single command, either add the following as well:

command! -nargs=+ Foldsearch exe "normal /".<q-args>."^M\z"

Or get rid of the \z entirely:

command! -nargs=+ Foldsearch exe "normal /".<q-args>."^M" | setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2

In these last two code segments, be sure to replace the "^M" with an actual CTRL-M (carriage return) character. Type :Folds[earch] <search string>[ENTER] to use this command. Use zr to display more context, use zm to display less context as before.

Customizing

To add a second level of context, you could add this to the end of foldexpr:

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

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

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

foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum)=~@/)\|\|(getline(v:lnum)=~@/)?0:1

Uses

Aside from the possible convenience of seeing only the search terms and their immediate context, this method of folding can be used for practical purposes as well. For example, viewing a "quick and dirty" api of a source code file.

To make a command to do a quick Java API for example, use:

" View the methods and variables in a java source file."
command! Japi Foldsearch public\s\|protected\s\|private\s

Note that a better way to do this for languages that support it would be to use syntax highlighting. Check the syntax file for the language in question to determine if this is an option. Many syntax files such as those for C, Perl, and VimL all define rules for at least some syntax-based folding, using :set foldmethod=syntax. Other languages such as Java do not currently have this functionality built in, so keep this idea around just in case you need it!

See Also

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 the foldexpr string does.