Skip to content

Instantly share code, notes, and snippets.

@lvv
Last active December 11, 2015 05:48
Show Gist options
  • Save lvv/4554184 to your computer and use it in GitHub Desktop.
Save lvv/4554184 to your computer and use it in GitHub Desktop.
Table-of-contents Fold
" table of contents fold
" fold togle
nmap <F8> :hi clear Folded<CR>:call Fold()<CR>
" next/pref fold
nmap { kzkjz<CR>
nmap } zjz<CR>
set nofoldenable " start vim with folding off
hi Folded term=bold ctermfg=8 ctermbg=0
set foldmethod=expr
set foldexpr=FoldExpr_fun('^[/a-zA-Z#]') " regex for C++
fun FoldExpr_fun (regex)
if (getline(v:lnum) =~ a:regex)
return ( ! (getline(v:lnum-1) =~ a:regex) ? ">1" : "1" )
else
return ( (getline(v:lnum+1) =~ a:regex) ? "<1" : "1" )
endif
endf
set foldtext=TitleLineDisplay()
set fillchars=""
fun TitleLineDisplay() " Returns a String, to be displayed for a closed fold
let line = getline(v:foldstart)
return line
endf
" toggler
fun Fold()
if &foldenable
exe "normal znzt"
set foldlevel=0
else
exe "normal zNz."
end
endf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment