Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

By default Vim seems to not want to indent the contents inside of <li> tags, though it autoindents properly for most other HTML tags.

For example, if I start with this code:

<ul>
<li>
foo
</li>
<li>
bar
</li>
</ul>

and have vim autoindent it I get:

<ul>
  <li>
  foo
  </li>
  <li>
  bar
  </li>
</ul>

However, what I really want is this:

<ul>
  <li>
    foo
  </li>
  <li>
    bar
  </li>
</ul>

It's kind of annoying when writing new code to have it autoindent after most opening tags but not this one, though that's easy enough to work around. Where this is really getting to me is when using vim to autoformat some large generated HTML that I'm trying to play around with (trying to mock up some UI changes using the generated source).

Is there any easy way to change this autoindent behavior so that it treats <li> just like any other opening tag, and indent the contents?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

I don't see an easy way to do it, but this solution isn't too difficult.

  1. Copy $VIMRUNTIME/indent/html.vim to ~/.vim/indent/html.vim if you're on Unix or to ~/vimfiles/indent/html.vim if you're on Windows.
  2. Edit your copy of indent/html.vim, adding this line,

    call <SID>HtmlIndentPush('li')

    to the list of similar calls already in that file.

That should do it.

share|improve this answer
    
I think that falls under the category of "easy fix". I figured it had to be something like that, but wasn't sure how the indent stuff was defined. Worked perfectly. –  Herms Jul 14 '11 at 14:13
    
I'm using built in vim on OSX. Any idea what $VIMRUNTIME is? vim is at /usr/bin/ (not a symlink) –  John Hinnegan Apr 28 '13 at 6:21
1  
@JohnHinnegan: You should be able to open Vim and execute :echo $VIMRUNTIME. –  garyjohn Apr 29 '13 at 5:11
    
@garyjohn thank you –  John Hinnegan Apr 29 '13 at 5:21

If anyone else finds this question like I did, via google, there is another solution using Tim Pope's rag-tag extension. This will automatically add the correct indentions and update some other tags for HTML5.

share|improve this answer
    
Sorry, I installed the plugin you mention but the <li>'s are not indented correctly for me.. I copied ragtag.vim to my ~/.vim/plugin/ directory, then restart vim, and gg=G –  user35538 Oct 8 '13 at 17:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.