Fandom

Vim Tips Wiki

Skip blank lines when folding text

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 122 Printable Monobook Previous Next

created September 27, 2001 · complexity intermediate · author Michael Brailsford · version 6.0


I love the text folding capabilities of Vim. I didn't like that it would display the first line of the range as the "title" for the fold. I like to write my comments with the "/*" on a line by itself. So I wrote this little function that will skip over anything that isn't a character, and then display whatever it finds after that character.

Just include this in your vimrc:

function GetFirstLineWithChars()
  let line_num = 0
  let charline = matchstr(getline(v:foldstart), '[a-zA-Z][a-zA-Z ]*')
  while strlen(charline) == 0
    let line_num = line_num + 1
    let charline = matchstr(getline(v:foldstart + line_num), '[a-zA-Z][a-zA-Z ]*')
  endw
  return charline
endfunction

set foldtext='+'.v:folddashes.substitute(GetFirstLineWithChars(),'\\\/\\\/\\\|\\*\\\|\\*\\\|{{{\\d\\=','','g')
set fillchars=fold:
hi folded guibg=black guifg=yellow gui=bold

And as an added bonus, for those new to text folding, add this to your vimrc file too:

autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview

That way whatever folds you set won't get lost when you quit. I had that happen after spending 15 minutes folding up a 3000 line file.

CommentsEdit

The line that looks like "set fillchars=fold:" has a space at the end. Simply type <C-k><Space><Space> to reproduce it. It's just a space.


Also on Fandom

Random Wiki