Asciidoc folding for Vim
I just learned about the ability to fold in Vim. For mere mortals, it means hiding parts of the file.
Here is a code to put in your .vimrc
to allow folding for Asciidoc(tor) files.
It folds asciidoc files at section boundaries and use nested folds for subsections.
"" Fold Asciidoc files at sections and using nested folds for subsections
" compute the folding level
function! AsciidocLevel()
if getline(v:lnum) =~ '^== .*$'
return ">1"
endif
if getline(v:lnum) =~ '^=== .*$'
return ">2"
endif
if getline(v:lnum) =~ '^==== .*$'
return ">3"
endif
if getline(v:lnum) =~ '^===== .*$'
return ">4"
endif
if getline(v:lnum) =~ '^====== .*$'
return ">5"
endif
if getline(v:lnum) =~ '^======= .*$'
return ">6"
endif
return "="
endfunction
" run the folding level method when asciidoc is here
autocmd Syntax asciidoc setlocal foldexpr=AsciidocLevel()
" enable folding method: expression on asciidoc
autocmd Syntax asciidoc setlocal foldmethod=expr
" start with text unfolded all the way
autocmd BufRead *.adoc normal zR
autocmd BufRead *.asciidoc normal zR
" TODO following does not work as folding is lost up reloading
" autocmd Syntax asciidoc normal zR
I’m sure it can be improved - I’d love to fold blocks as well - but that’s a start.
Here are a few commands to remember to fold in Vim
zo
: open a fold at cursorzO
: open all folds down at cursorzc
: close a fold at cursorzC
: close all levels of folds at cursorza
: /zA
: toggle foldzm
: close fold by one level across the filezM
: close all folds across the filezr
: open fold by one level across the filezR
: open all folds across the filezj
/zk
: move to next / previous fold[z
/]z
: go to begining / end of the fold
Happy folding.
Comments