Indentation in erb breaks for nested <% %> tags #146

Closed
calebthompson opened this Issue Mar 25, 2013 · 6 comments

Projects

None yet

5 participants

@calebthompson

Indentation for erb's <% %> tags breaks when nested within html tags.

When I write this code:

<nav class='header'>
  <% if current_user.present? %>
    <a href='/sign_out'>Sign out</a>
  <% end %>
</nav>

And run gg=G to indent the entire file, vim dedents the <% end %> tag to this result:

<nav class='header'>
  <% if current_user.present? %>
    <a href='/sign_out'>Sign out</a>
<% end %>
</nav>

Migrated from tpope/vim-rails#231. I checked issues here before reposting, but I'm not seeing anything related.

@tpope
Vim-Ruby member

My results are different.

<nav class='header'>
<% if current_user.present? %>
  <a href='/sign_out'>Sign out</a>
<% end %>
</nav>

That's because the HTML indenting algorithm hasn't been touched in 10 years and doesn't know about HTML5. This fixes it:

let g:html_indent_tags .= '\|nav'

This happens to be wrapped up in ragtag.vim if you are looking for a ready-baked solution.

Reopen if this doesn't fix the issue for you.

@tpope tpope closed this Mar 25, 2013
@calebthompson

I tried adding that to the .vimrc:

Error detected while processing /Users/caleb/.vimrc.local:
line  150:
E121: Undefined variable: g:html_indent_tags
Press ENTER or type command to continue

I got similar results trying to add it within vim with :let g:html_indent_tags .= '\|nav'

When I went to add ragtag.vim, I noticed that I didn't actually have vim-ruby installed, just vim-rails. Adding vim-ruby to my .vimrc.bundles (for vundle) mostly solved this issue.

@graywh

@calebthompson .= is VimL's += for strings. You're getting that error because g:html_indent_tags doesn't exist yet. In that case, you could use :let g:html_indent_tags = 'nav', but your adding ragtag.vim is probably what solved it.

@tpope
Vim-Ruby member

You actually need to set it in after/indent/html.vim. It's a global variable, but the runtime file clobbers it each time.

@graywh

I just saw that.

Edit: That's messed up.

@ilyakatz

https://github.com/othree/html5.vim help with some html5 issues

@dkearns dkearns added the indent label Sep 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment