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

五迷三道 提交于 2019-12-11 01:48:25

问题


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 ?


回答1:


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.


来源:https://stackoverflow.com/questions/19036536/vim-folding-how-to-hide-all-the-single-lines-not-containing-a-search-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!