如何使用Vim折叠/展开HTML标记

如何使用Vim折叠/展开HTML标记,html,vim,folding,fold,Html,Vim,Folding,Fold,是否有一些插件可以在Vim中折叠HTML标记 或者有另一种方法来设置折叠或展开html标记的快捷方式 我想像缩进折叠一样折叠/展开html标记。我发现zfat(或者同样地,zfit)可以很好地折叠html文档za将切换(打开或关闭)现有折叠zR打开当前文档中的所有折叠,zM有效地重新启用文档中标记的所有现有折叠 如果您发现自己在大量使用折叠,您可以在.vimrc.中为自己制作一些方便的键绑定。如果您缩进HTML,以下内容应该可以使用: set foldmethod=indent 我发现,问题是

是否有一些插件可以在Vim中折叠HTML标记
或者有另一种方法来设置折叠或展开html标记的快捷方式

我想像缩进折叠一样折叠/展开html标记。

我发现
zfat
(或者同样地,
zfit
)可以很好地折叠html文档
za
将切换(打开或关闭)现有折叠
zR
打开当前文档中的所有折叠,
zM
有效地重新启用文档中标记的所有现有折叠


如果您发现自己在大量使用折叠,您可以在
.vimrc.

中为自己制作一些方便的键绑定。如果您缩进HTML,以下内容应该可以使用:

set foldmethod=indent
我发现,问题是褶皱太多了。为了解决这个问题,我分别使用
zO
zc
打开和关闭嵌套褶皱

有关更多信息,请参见帮助折叠缩进:

The folds are automatically defined by the indent of the lines.

The foldlevel is computed from the indent of the line, divided by the
'shiftwidth' (rounded down).  A sequence of lines with the same or higher fold
level form a fold, with the lines with a higher level forming a nested fold.

The nesting of folds is limited with 'foldnestmax'.

Some lines are ignored and get the fold level of the line above or below it,
whichever is lower.  These are empty or white lines and lines starting
with a character in 'foldignore'.  White space is skipped before checking for
characters in 'foldignore'.  For C use "#" to ignore preprocessor lines.

When you want to ignore lines in another way, use the 'expr' method.  The
indent() function can be used in 'foldexpr' to get the indent of a line.
安装命令(JavaScript版本)

原始html:

js美化和vim折叠:


添加到James Lai的答案中。 最初我的foldmethod=语法,因此zfat无法工作。 解决方案是将foldemethod设置为手动

:setlocal foldmethod=manual
要检查正在使用的折叠方法

:setlocal foldmethod?

使用foldmethod语法折叠html,这更简单

这个答案是基于。作者是@Ingo Karcat

  • 使用以下命令将折叠方法设置为语法:

    vim命令行
    :设置foldmethod=syntax

    或者将设置置于
    ~/.vim/after/ftplugin/html.vim

    setlocal foldmethod=syntax
    
  • 还要注意,到目前为止,默认语法脚本仅折叠多行 标记本身,而不是开始标记和结束标记之间的文本

        So, this gets folded:
    
            <div
                class="foo"
                id="bar"
            > 
    
        And this doesn't
    
            <div>
                <b>text between here</b>
            </div>
    

  • 首先
    set foldmethod=syntax
    并尝试
    zfit
    折叠开始标签和
    zo
    展开标签,它在我的vim上运行良好。

    尝试set fdl=3关闭默认的三级折叠和更高的折叠;zc仍然关闭打开的折叠。
    set foldmethod=syntax
    适用于HTML@HaiFengKao你如何使用这一套?@nilon
    zc
    关闭折叠
    zo
    打开一个折叠。你的答案只适用于手册的折叠方法,不能适用于其他折叠方法。一旦我打开文件,所有这些都有效。一旦关闭和重新打开,我必须重新开始。为了避免这种情况?也就是说:文件记录了折叠的行,因此视图从关闭到重新打开是相等的。
        So, this gets folded:
    
            <div
                class="foo"
                id="bar"
            > 
    
        And this doesn't
    
            <div>
                <b>text between here</b>
            </div>
    
    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