Wikia

Vim Tips Wiki

Skip blank lines when folding text

Talk0
1,610pages on
this wiki

Redirected from VimTip122

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.


Advertisement | Your ad here

Around Wikia's network

Random Wiki