Vim .html.erb 的奇怪缩进
我正在输入 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Vim 的默认 html 缩进不会缩进
标签。这意味着,它不仅不会在
之后删除缩进级别,而且也可能不会在打开
之后自动添加缩进级别;
。如果是这种情况,您可以通过设置变量g:html_indent_tags
来更改此行为。它应该包含与标签名称匹配的正则表达式。例如:这将为
p
、li
和nav
标记添加缩进级别。如果您只需要标签,只需将其设置为“p”:
如果 vim 确实正在缩进初始
,那么您的
indentkeys
选项可能不包含“>”特点。您可以通过执行set indentkeys
来检查其内容。如果它不包含<>>
,您可以将其添加到.vim/ftplugin/html.vim
中:编辑:
不幸的是,vim 似乎取消了该变量...这对我来说根本没有意义,但你可以做的一件事就是将该变量赋值添加到
.vim/after/ftplugin/html.vim
相反。这应该可以解决问题。就我个人而言,我做了一些不同的事情——我将默认文件复制到.vim/indent/html.vim
并注释掉删除变量的行。尽管如此,使用after
目录可能是一个更好的主意。编辑:
html5 插件似乎不受此问题的影响。直接安装它可能是个好主意。否则,
g:html_indent_tags
变量仍然是可以去的地方,但最好的位置可能是~/.vim/after/indent/html.vim
:使用
.=
而不是=
。这是就地串联。您需要它,因为此时该变量已经存在并且您不想删除它。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 variableg:html_indent_tags
. It should contain a regular expression that matches the tag's name. For example:This will add a level of indent for the
p
,li
andnav
tags. If you want the<p>
tags only, just set it to "p":If vim really is indenting the initial
<p>
, then it's possible that yourindentkeys
option doesn't contain the ">" character. You can check its contents by executingset indentkeys
. If it doesn't contain<>>
, you could add it in.vim/ftplugin/html.vim
: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 theafter
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
: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.我也有同样的问题。 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
我通过万能的谷歌得到了这个问题。
为了补充 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!