vimtricks.wiki Concise Vim tricks, one at a time.

How do I customize the text displayed for closed folds in Vim?

Answer

:set foldtext={expr}

Explanation

The foldtext option controls what Vim displays for a closed fold. By default it shows a line like +-- 42 lines: function foo() ----------. You can replace this with any Vimscript expression that returns a string, enabling cleaner or more informative fold summaries.

How it works

The expression in foldtext has access to special variables:

  • v:foldstart — line number of the first line in the fold
  • v:foldend — line number of the last line in the fold
  • v:folddashes — a string of dashes matching the fold depth
  • v:foldlevel — the fold's nesting level

Example

Default fold display:

+--  12 lines: function processData() {---------------

With a custom foldtext:

set foldtext=getline(v:foldstart).'\t['.((v:foldend-v:foldstart)).' lines]'

Result:

function processData() {        [11 lines]

For a minimal style that just shows the first line:

set foldtext=getline(v:foldstart)

Tips

  • Combine with set fillchars+=fold: (a space) to remove the trailing dashes
  • For complex logic, define a function and reference it: :set foldtext=MyFoldText()
  • The expression is re-evaluated each time a fold is displayed, so dynamic content is possible
  • Works with any foldmethod (indent, marker, syntax, expr, manual)

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?