Vim .html.erb 的奇怪缩进

发布于 2024-12-03 04:18:39 字数 413 浏览 1 评论 0原文

我正在输入 .html.erb 文件,我意识到 vim 缩进的这种奇怪行为。

<p>
  <strong>Expires On:</strong>
  <%= @item.expires_on %>
</p>

当我在

之后按 Enter 时,为什么会发生这种情况?

<p>
  <strong>Expires On:</strong>
  <%= @item.expires_on %>
  </p>
  _ <= new cursor space 

请注意,我确实有文件类型缩进

I am typing in my .html.erb file and I realize this weird behaviour of vim indentation.

<p>
  <strong>Expires On:</strong>
  <%= @item.expires_on %>
</p>

How come when I press enter after </p> this happens?

<p>
  <strong>Expires On:</strong>
  <%= @item.expires_on %>
  </p>
  _ <= new cursor space 

Note that I do have filetype indent on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风启觞 2024-12-10 04:18:39

Vim 的默认 html 缩进不会缩进

标签。这意味着,它不仅不会在

之后删除缩进级别,而且也可能不会在打开

之后自动添加缩进级别;。如果是这种情况,您可以通过设置变量 g:html_indent_tags 来更改此行为。它应该包含与标签名称匹配的正则表达式。例如:

let g:html_indent_tags = 'p\|li\|nav'

这将为 plinav 标记添加缩进级别。如果您只需要

标签,只需将其设置为“p”:

let g:html_indent_tags = 'p'

如果 vim 确实正在缩进初始

,那么您的 indentkeys 选项可能不包含“>”特点。您可以通过执行set indentkeys来检查其内容。如果它不包含 <>>,您可以将其添加到 .vim/ftplugin/html.vim 中:

setlocal indentkeys+=<<>

编辑

不幸的是,vim 似乎取消了该变量...这对我来说根本没有意义,但你可以做的一件事就是将该变量赋值添加到 .vim/after/ftplugin/html.vim 相反。这应该可以解决问题。就我个人而言,我做了一些不同的事情——我将默认文件复制到 .vim/indent/html.vim 并注释掉删除变量的行。尽管如此,使用 after 目录可能是一个更好的主意。

编辑

html5 插件似乎不受此问题的影响。直接安装它可能是个好主意。否则,g:html_indent_tags 变量仍然是可以去的地方,但最好的位置可能是 ~/.vim/after/indent/html.vim

let g:html_indent_tags .= '\|p\|nav\|othertags'

:使用 .= 而不是 =。这是就地串联。您需要它,因为此时该变量已经存在并且您不想删除它。

Vim's default html indentation doesn't indent <p> tags. This means that, not only would it not remove a level of indent after </p>, but it also probably doesn't add a level of indent automatically after the opening <p>. If that's the case, you can change this behaviour by setting the variable g:html_indent_tags. It should contain a regular expression that matches the tag's name. For example:

let g:html_indent_tags = 'p\|li\|nav'

This will add a level of indent for the p, li and nav tags. If you want the <p> tags only, just set it to "p":

let g:html_indent_tags = 'p'

If vim really is indenting the initial <p>, then it's possible that your indentkeys option doesn't contain the ">" character. You can check its contents by executing set indentkeys. If it doesn't contain <>>, you could add it in .vim/ftplugin/html.vim:

setlocal indentkeys+=<<>

EDIT:

Unfortunately, vim seems to unlet that variable... This doesn't make sense to me at all, but one thing you could do is add that variable assignment to .vim/after/ftplugin/html.vim instead. This should do the trick. Personally, I've done something different -- I've copied the default file to .vim/indent/html.vim and commented out the lines that remove the variable. Still, using the after directory is probably a better idea.

EDIT:

The html5 plugin seems not to suffer from this issue. It could be a good idea to just install that instead. Otherwise, the g:html_indent_tags variable is still the place to go, but the best place for it is probably ~/.vim/after/indent/html.vim:

let g:html_indent_tags .= '\|p\|nav\|othertags'

Note the usage of .= instead of =. This is in-place concatenation. You need it, since the variable already exists at this point and you don't want to delete it.

筱武穆 2024-12-10 04:18:39

我也有同样的问题。 Tim Pope 有一个出色的 Vim 插件,可以为默认情况下缺少的内容添加缩进。 https://github.com/tpope/vim-ragtag

I had the same problem. Tim Pope has an excellent Vim plugin that adds indentation for things missing by default. https://github.com/tpope/vim-ragtag

可是我不能没有你 2024-12-10 04:18:39

我通过万能的谷歌得到了这个问题。

为了补充 Andrew 上面所说的内容,对于那些使用 OSX 的人,您可能想看一下 /Applications/MacVim.app/Contents/Resources/vim/runtime/indent/html.vim ,必要的修改应该会变得显而易见。

我不敢相信我花了这么多时间来忍受缩进的

  • 的问题!
  • I got to this question via the Almighty Google.

    To complement on what Andrew said above, for those of you on OSX you might want to take a peek at /Applications/MacVim.app/Contents/Resources/vim/runtime/indent/html.vim and the modifications necessary should become evident.

    I can't believe I spent so much time suffering from poorly indented <li>s!

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文