The 'foldmethod'
is a window-local option; setting it from your ~/.vimrc
doesn't necessarily have the right effect.
Because syntax folding is bound to the html
filetype, these settings belong to ~/.vim/after/ftplugin/html.vim
:
setlocal foldmethod=syntax
This depends on having filetype plugin on
in your ~/.vimrc
, which you probably have. You can also configure other related options there, e.g. foldcolumn=4
.
Also note that so far (as of Vim 7.4.1830), the default HTML syntax script only folds a multi-line tag itself, not the text between the opening and closing tag.
So, this gets folded:
<div
class="foo"
style="width: 100"
>
But this doesn't:
<div>
<b>stuff in between</b>
</div>
To get this, you need to extend the syntax script, e.g. via the following, best placed into ~/.vim/after/syntax/html.vim
:
Alternative 1
Folding is performed between all but void html elements (those which don't have a closing sibling, like <br>
). Contributed by @zanona; thanks!
syntax region htmlFold start="<\z(\<\(area\|base\|br\|col\|command\|embed\|hr\|img\|input\|keygen\|link\|meta\|para\|source\|track\|wbr\>\)\@![a-z-]\+\>\)\%(\_s*\_[^/]\?>\|\_s\_[^>]*\_[^>/]>\)" end="</\z1\_s*>" fold transparent keepend extend containedin=htmlHead,htmlH\d
Alternative 2
Folding is performed between certain explicitly named structural (e.g. <head>
), paragraph-level (e.g. <p>
, <li>
) and ancillary (e.g. <script>
) HTML tags.
syntax region htmlFold start="<\z(p\|h\d\|i\?frame\|table\|colgroup\|thead\|tfoot\|tbody\|t[dhr]\|pre\|[diou]l\|li\|span\|div\|head\|script\|style\|blockquote\|form\)\%(\_s*\_[^/]\?>\|\_s\_[^>]*\_[^>/]>\)" end="</\z1\_s*>" fold transparent keepend extend containedin=htmlHead,htmlH\d