开发者

Vim Weird indentation for .html.erb

开发者 https://www.devze.com 2023-04-02 18:20 出处:网络
I am typing in my .html.erb file and I realize this weird behaviour of vim indentation. <p> <strong>Expires On:</strong>

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.


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.


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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号