Question

Despite using Vim for a decade, sometimes I still struggle with folding. Everything in this question is reproducible with no .vimrc in Vim 7.3. I am using foldmethod=marker with default foldmarkers.

Consider the following nested fold structure (using foldmethod=marker and default markers). The first column is the line number (fold0 has zero indent).

 1|{{{ fold0
 2|    {{{ fold1
 3|        {{{ fold2
 4|        }}}
 5|    }}}
 6|}}}

Suppose fold1 and fold2 are both closed and the cursor is on line 2. I can yank and put the closed fold using yyp, but the newly inserted folds are all open.

What I would like is for folds resulting from a "put" command to be closed. Alternatively, is there a convenient way to close them after the "put"? That is, from this position (with the cursor on line 6):

 1|{{{ fold0
 2|+---  4 lines: fold1----------------------------------------
 6|    {{{ fold3
 7|        {{{ fold4
 8|        }}}
 9|    }}}
10|}}}

I can use zc to close fold3, but fold 4 remains open after zo. If instead I use zC, it closes fold3 and fold 0, but fold 4 is still open when I do 2zo. This is not how I would expect recursive fold-closing to work. Is there a way to achieve zc but also recursively closing all contained folds?

The company I work for specifies that folds are defined by matching pairs without an explicit foldlevel, so solutions involving explicit foldlevel are of no use.

Was it helpful?

Solution

The following sequence (after your yyp) seems to do what you want:

v% - visually select from the { under the cursor to the corresponding }

zC - close all folds under the cursor recursively (unfortunately, this includes the outer folds that you want to remain open)

zv - open just enough folds to display the cursor line

zc - close the single outer fold of the new material

Of course, you'd want to map this to some other key combination in your vimrc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top