Vim folding: how to hide all individual lines that do not contain a search pattern (or collapse the zero line)?

I have text files that are just paragraphless lists. When I want to focus on an element, I can dump everything but matches into my search, thanks to Vim Wikia (Tip 282: โ€œSimple foldโ€):

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

This turns out to be useful: in this way, I can very clearly see the objects I'm looking for: they appear in white on a black background, while the folds are dark gray (ctermfg) on โ€‹โ€‹gray (ctermbg).

But there is - a minor - glitch. It can happen (and, in fact, it often happens) that one line that does not contain a pattern stays between two lines that contain a pattern, for example:

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

A simple fold will fold lines 2 and 3, not line 5.

How should I hide this single line?

( , ...)? , .

, (, , ) ?

+2
1

set :

set fml=0

:

:h 'fml' 

:

 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.
+6

Source: https://habr.com/ru/post/1628181/


All Articles