" Javadoc comments (/** and */ pairs) and code sections (marked by {} pairs) mark the start and end of folds. All other " lines simply take the fold level that is going so far. function! MyFoldLevel( lineNumber ) let thisLine = getline( a:lineNumber ) " If the entire Javadoc comment or the {} pair is on one line, then don't create a fold for it. if ( thisLine =~ '\%(\%(/\*\*\).*\%(\*/\)\)\|\%({.*}\)' ) return '=' elseif ( thisLine =~ '\%(^\s*/\*\*\s*$\)\|{' ) return "a1" elseif ( thisLine =~ '\%(^\s*\*/\s*$\)\|}' ) return "s1" endif return '=' endfunction setlocal foldexpr=MyFoldLevel(v:lnum) setlocal foldmethod=expr