sponsor Vim development Vim logo Vim Book Ad

searchfold.vim : Fold away lines not matching the last search pattern

 script karma  Rating 49/13, Downloaded by 2033  Comments, bugs, improvements  Vim wiki

created by
Andy Wokula
script type
This script is an improved version of  f.vim  (vimscript #318).

Improvements: folding depth is limited, previous fold settings can be restored, pattern is always the last search pattern.

Script uses manual folds.  The user's manual folds are not preserved (unlike vimscript #2302 does it).


fold away lines not matching the last search pattern.
With [count], change the initial foldlevel to ([count] minus one).  The setting will be stored in g:searchfold_foldlevel and will be used when [count] is omitted.

fold away lines that do match the last search pattern (inverse folding).

try hard to restore the previous fold settings


:let g:searchfold_maxdepth = 7
(number) maximum fold depth

:let g:searchfold_usestep = 1
Controls how folds are organized: If 1 (default), each "zr" (after "\z") unfolds the same amount of lines above and below a match.  If 0, only one more line is unfolded above a match.  This applies for next "\z" or "\iz".

:let g:searchfold_postZ_do_zv = 1
If 1, execute "zv" (view cursor line) after <Leader>Z.

:let g:searchfold_foldlevel = 0
Initial 'foldlevel' to set for <Leader>z and <Leader>iz

:let g:searchfold_do_maps = 1
Whether to map the default keys or not.

A few more links to check out:
- http://www.noah.org/wiki/Vim#Folding
- vimscript #158 (foldutil.vim)
- vimscript #578 (allfold.tar.gz)

2011 May 24
install details
this is a global plugin, :h add-plugin

which means:
- remove the version part "_0.9" from the file name (optional)
- drop the script file into your plugin folder (e.g. ~/vimfiles/plugin) and restart Vim, or :source the script manually

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
searchfold_0.9.vim 0.9 2011-05-24 7.0 Andy Wokula renamed <Plug>SearchFoldDisable, fixed g:searchfold_usestep description (wrong default), removed annoying redraw
searchfold_0.8.vim 0.8 2010-09-30 7.0 Andy Wokula added inverse folding (<Leader>iz), g:searchfold_foldlevel, count for <Leader>z, <Plug> mappings, disabled F(), minor fixes
searchfold_0.7.vim 0.7 2009-01-22 7.0 Andy Wokula Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github