I have just recently got irritated enough with the current way that folds appear in Vim that I decided to roll my own custom foldtext function.
In particular I wanted the top (non-blank) line to be displayed clearly and cleanly. I also wanted to know not just how many lines were folded but what percentage of the file this accounts for. This is often useful for quickly glancing at to see where most of the document resides. I also wanted a nice easy way of visualising the fold level and how that compares to the folds surrounding a fold.
This is the end result.
1 fu! CustomFoldText() 2 "get first non-blank line 3 let fs = v:foldstart 4 while getline(fs) =~ '^\s*$' | let fs = nextnonblank(fs + 1) 5 endwhile 6 if fs > v:foldend 7 let line = getline(v:foldstart) 8 else 9 let line = substitute(getline(fs), '\t', repeat(' ', &tabstop), 'g') 10 endif 11 12 let w = winwidth(0) - &foldcolumn - (&number ? 8 : 0) 13 let foldSize = 1 + v:foldend - v:foldstart 14 let foldSizeStr = " " . foldSize . " lines " 15 let foldLevelStr = repeat("+--", v:foldlevel) 16 let lineCount = line("$") 17 let foldPercentage = printf("[%.1f", (foldSize*1.0)/lineCount*100) . "%] " 18 let expansionString = repeat(".", w - strwidth(foldSizeStr.line.foldLevelStr.foldPercentage)) 19 return line . expansionString . foldSizeStr . foldPercentage . foldLevelStr 20 endf
And these are the folds that it produces.