Is it possible to display nested sections in a collapsed fold with vim?

淺唱寂寞╮ 提交于 2019-12-24 04:10:38

问题


When a fold is collapsed in vim, all of the nested headings are hidden away so that you can't see what's inside. I'm curious if anyone knows if it's possible or has a solution for foldtext function (or through another method) that can display the sections inside a fold when a fold is collapsed.

I'm looking for something that would display folds more like this:

+ --   2000 TopSection1                                    " Fold Level 1
+ ---   500 TopSection1 : ChildSection1                    " Fold Level 2
+ ----   50 TopSection1 : ChildSection1 : BottomSection1   " Fold Level 3
+ ---   100 TopSection1 : ChildSection2 : BottomSection1   " Fold Level 2
+ --    500 TopSection2                                    " Fold Level 1
+ ---    25 TopSection2 : ChildSection1                    " Fold Level 2

I've been digging around, but have not figured out a method to make this work (or if it's possible). Any suggestions?


回答1:


You will have to play with foldtext but also to parse the content of the section to fetch what you want to display.




回答2:


the following command gets all folding lines without the body texts in between :

:g/{{{/

It works for this example below that contains multiple nested folds with foldmethod=marker and default ({{{) mark :

Text 1/*{{{*/
some text here
subtext 1.1/*{{{*/
some text here
subsubtext 1.1.1/*{{{*/
some text here/*}}}*/
subsubtext 1.1.2/*{{{*/
some text here/*}}}*//*}}}*/
subtext 1.2/*{{{*/
some text here
subsubtext 1.2.1/*{{{*/
some text here/*}}}*/
subsubtext 1.2.2/*{{{*/
some text here/*}}}*//*}}}*//*}}}*/
Text 2/*{{{*/
some text here
subtext 2.1/*{{{*/
some text here
subsubtext 2.1.1/*{{{*/
some text here/*}}}*/
subsubtext 2.1.2/*{{{*/
some text here/*}}}*//*}}}*/
subtext 2.2/*{{{*/
some text here
subsubtext 2.2.1/*{{{*/
some text here/*}}}*/
subsubtext 2.2.2/*{{{*/
some text here/*}}}*//*}}}*//*}}}*/

After you run the :g/{{{/ command, you get this :

Text 1/*{{{*/
subtext 1.1/*{{{*/
subsubtext 1.1.1/*{{{*/
subsubtext 1.1.2/*{{{*/
subtext 1.2/*{{{*/
subsubtext 1.2.1/*{{{*/
subsubtext 1.2.2/*{{{*/
Text 2/*{{{*/
subtext 2.1/*{{{*/
subsubtext 2.1.1/*{{{*/
subsubtext 2.1.2/*{{{*/
subtext 2.2/*{{{*/
subsubtext 2.2.1/*{{{*/
subsubtext 2.2.2/*{{{*/

If you want to redirect the result to a new buffer, then you can run :

:let @a='' | execute 'g/{{{/y A' | new | setlocal bt=nofile | put! a

It yanks the {{{ pattern to register "a", opens a new buffer and pastes the reg. You may then need to expand the result with zR if your default is 'collapse folds'.




回答3:


I use zr and zm normal commands to open and close another folding level. I agree that zr will also show text between a folding level and a sub-level ; hence it does not fully address your question. It's seems that a better way would be to use foldmethod=syntax and then filter all folding lines with a global (g) command based on the regex of the foldmethod syntax.



来源:https://stackoverflow.com/questions/27392059/is-it-possible-to-display-nested-sections-in-a-collapsed-fold-with-vim

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