68

是否有一些插件可以在 Vim 中折叠 HTML 标签?
或者还有另一种方法来设置折叠或展开 html 标签的快捷方式?
我想像缩进折叠一样折叠/展开 html 标签。

4

6 回答 6

93

我发现zfat(或者,同样,zfit)非常适合折叠 HTML 文档。za将切换(打开或关闭)现有折叠。zR打开当前文档中的所有折叠,zM有效地重新启用文档中标记的所有现有折叠。

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

于 2012-09-12T02:55:58.283 回答
25

如果您缩进 HTML,则以下内容应该有效:

set foldmethod=indent

我发现这个问题是折叠太多了。为了解决这个问题,我分别使用zOzc打开和关闭嵌套折叠。

有关help fold-indent更多信息,请参阅:

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.
于 2013-06-29T21:20:33.937 回答
7

用 foldmethod 语法折叠 html,更简单。

这个答案基于vim 中的 HTML 语法折叠。作者是@Ingo Karcat。

  1. 将您的 fold 方法设置为具有以下内容的语法:

    vim 命令行:set foldmethod=syntax

    或将设置放入~/.vim/after/ftplugin/html.vim

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

        So, this gets folded:
    
            <div
                class="foo"
                id="bar"
            > 
    
        And this doesn't
    
            <div>
                <b>text between here</b>
            </div>
    
  3. 要在标签之间折叠,您需要通过以下最佳位置扩展语法脚本~/.vim/after/syntax/html.vim

    语法折叠在除 void html 元素之外的所有元素之间执行(那些没有关闭兄弟的元素,如<br>

    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
    
于 2016-07-09T05:51:20.170 回答
4

安装js-beautify命令(JavaScript版)

npm -g install js-beautify  
wget --no-check-certificate https://www.google.com.hk/ -O google.index.html  
js-beautify -f google.index.html  -o google.index.bt.html  

http://www.google.com.hk原始 html:

http://www.google.com.hk 原文

js-beautify 和 vim 折叠:

js-beautify 和 vim 折叠

于 2013-10-16T07:44:50.427 回答
2

加入 James Lai 的回答。最初我的 foldmethod=syntax 所以 zfat 不起作用。解决方法是将foldemethod设置为手动

:setlocal foldmethod=manual

检查正在使用的折叠方法,

:setlocal foldmethod?
于 2014-02-15T05:04:48.930 回答
1

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

于 2017-10-03T13:03:44.187 回答