Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Weird indentation for .html.erb

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.

like image 344
disappearedng Avatar asked Sep 06 '11 23:09

disappearedng


2 Answers

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.

like image 127
Andrew Radev Avatar answered Sep 29 '22 05:09

Andrew Radev


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

like image 42
Rayne Avatar answered Sep 29 '22 06:09

Rayne