Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim folding : how to hide all the single lines not containing a search pattern (or fold zero line)?

Tags:

vim

folding

I have text files which are simply lists with no paragraphs. When I want to focus on an item, I am able to fold everything except for matches to my search, thanks to Vim Wikia (Tip 282 :"Simple Folding") :

:set foldexpr=getline(v:lnum)!~@/
:nnoremap <F8> :set foldmethod=expr<CR><Bar>zM

This proves to be useful : thus I can see very clearly the items I am looking for : they appear in white on a black background, whereas the folds are darkgrey (ctermfg) on grey (ctermbg).

But there is a - minor - glitch. It may happen (and, in fact, it often happens) that a single line not containing the pattern remains between two lines containing the pattern, eg :

1 pattern
2 not pattern
3 not pattern
4 pattern
5 not pattern
6 pattern

Simple foldind will fold away lines 2 and 3, not line 5.

How should I proceed to hide away this single line ?

Is there a way to fold zero line (this reminds me of the koan about one hand clapping…) ? I suppose that this is not possible.

So, is there a way to simply hide the line (e.g. with the same highlighting as the folds) with a function ?

like image 436
ThG Avatar asked Dec 07 '25 08:12

ThG


1 Answers

try to set another option:

set fml=0

for details about this option:

:h 'fml' 

relevant to your question:

 With the default value of
    one a fold can only be closed if it takes up two or more screen lines.
    Set to zero to be able to close folds of just one screen line.
like image 57
Kent Avatar answered Dec 09 '25 03:12

Kent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!