Folding is useful to hide away parts of the document. A visual representation of the folding structure as a tree would be useful for understanding the document structure at a glance.

1 Answer 1


You can use :set foldcolumn=<number> to display a <number>-wide column that tries to visualize fold levels. It would look something like this, by default (with a marker fold method, using {{{ and }}}):

-    {{{ Top Level Fold
|-   {{{ Nested Fold
||-  {{{ Deepest Fold
|||  }}}
||   }}}
|-   {{{ Another Nested Fold
||   }}}
|    }}}

However, this consumes quite a lot of horizontal real-estate. Another method would be to write a custom foldtext() implementation which reads the fold level with foldlevel() and formats accordingly. This will waste less horizontal space, but won't really be as helpful for visually understanding the tree since you can't see the folds underneath a collapsed fold. For example, your .vimrc:

set foldtext=CustomFoldText()
function! CustomFoldText()
  let line = getline(v:foldstart) . "..."
  return repeat("--| ", v:foldlevel) . line

There are plugins that provide on-demand visualizations of the fold hierarchy, such as folddigest.vim, as well. They work, essentially, by parsing the fold level of every line, building up an appropriate data structure, and dumping a visualization of the data structure to a new buffer. You could probably adapt the technique to your own needs.

You may also want to look at plugins that provide "indent guides," such as this one. I've never personally used them, and I hear they cause some people performance issues since they generally involve generating lots of custom match highlights via matchadd(), but your mileage may vary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.