views:

1110

answers:

2

The other day my friend asked me how to fix the indentation of his huge html files which was all messed up.

I tried the usual "gg=G" command, which is what I use to fix the indentation of code files. However, it didn't seem to work right on HTML files. It simply removed all the formatting.

I also tried setting :filetype = xml, to see if tricking it into thinking this was an XML file would help but it still didn't do it.

I'm sure some VIM ninja knows how to do this.

+7  A: 

With "filetype indent on" inside .vimrc, vim indents html files quite nicely.

Simple example with a shiftwidth of 2:

<html>
  <body>
    <p>
    text
    </p>
  </body>
</html>
marcog
Copy paste the html of this questions page into a html file. Open with VIM, type "set smartindent", then "gg=G" and it doesn't fix the indenting of the file.
Simucal
It works for me. set ft=html<cr>set si<cr>gg=G<cr>. Formats this page quite well.
Don Reba
Did a bit of fiddling, seems it's actually "filetype indent on" that does the magic.
marcog
+1 verified that "filetype indent on/off" switches the magic on or off.
Wim Coenen
Yes, after setting smart indent, filetype=html, and and filetype ident on it worked for me.
Simucal
+1  A: 

Have you tried using the HTML indentation script on the Vim site?

JaredPar
+1, I didn't see that. It would be nice if I didn't need to run a script but it looks like this might be the only way.
Simucal