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

StackOverflow https://stackoverflow.com/questions/19036536

  •  29-06-2022
  •  | 
  •  

Question

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 ?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top