Vim 折叠自动在代码中引入奇怪的注释

Vim folding introduces strange comments into the code automatically

我注意到 vim 在我尝试折叠时在代码中引入了奇怪的注释。

以这段代码为例:

<div class="ev_i_ctg">
    <!-- Category Icon -->
    <div class="ev_ic_ty FOA-bannerimage"></div>
    <!-- Number of attachments -->
    <div class="ev_ic_n">2</div>
</div>

当我尝试使用命令 zfat 折叠外部标签时,它会相应地折叠,但还会添加额外的内容,如下所示的代码:

<div class="ev_i_ctg"><!--{-->
    <!-- Category Icon -->
    <div class="ev_ic_ty FOA-bannerimage"></div>
    <!-- Number of attachments -->
    <div class="ev_ic_n">2</div>
</div><!--}-->

问题似乎是随机发生的,没有任何模式(或者,至少,我找不到任何模式)。为什么会这样?

第一步是:help zf。这就解释了

This only works when 'foldmethod' is "manual" or "marker". Also see |fold-create-marker|.

接着,我们得到这样的解释:

Vim will append the start and end marker, as specified with 'foldmarker'. The markers are appended to the end of the line. 'commentstring' is used if it isn't empty.

因此,您已配置 基于标记的折叠,您的 HTML(-方言)文件类型集 'commentstring'<!--%s-->,并且Vim 将插入那个。

解决方法:换一种折叠方式;通常,基于语法的折叠与 HTML 一起使用。在 ~/.vim/after/ftplugin/html.vim 中,输入:

setlocal foldmethod=syntax