153

How do I 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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mmcdole
  • 91,488
  • 60
  • 186
  • 222

12 Answers12

217

There's several things that all need to be in place. Just to summarize them all in one location:

Set the following option:

:filetype indent on
:set filetype=html           # abbrev -  :set ft=html
:set smartindent             # abbrev -  :set si

Then either move the cursor to the top of the file and indent to the end: gg =G
Or select the desired text to indent and hit = to indent it.

tylerl
  • 30,197
  • 13
  • 80
  • 113
  • @tyrel, thanks, but for me is not working.. This is the file's content: http://pastebin.com/gagia8W2 . The file is called `home.html`. I don't have any problem to indent .php files. Here you have my .vimrc: http://pastebin.com/FAJ0MCA9 – tirenweb Apr 24 '13 at 18:24
  • 1
    Is there a way I can set this in the .vimrc? I don't want to tell it everytime i open an HTML file that its an HTML file. Thanks a lot for gg,=,G shortcut. Really handy. – zakishaheen Feb 28 '14 at 03:54
  • 1
    Note that in `vim 7.4` the default indentation settings will **fail** for this example, as `html`, `body`, and `p` are not indented by default. See [this answer](http://stackoverflow.com/a/19351416/446554). – Cory Klein Jun 09 '14 at 17:07
  • 2
    `smartindent` isn't necessary. Also it's tuned for C and C++, I prefer to use more general `autoindent` instead. – Kos Oct 21 '14 at 10:00
  • It works great with vim7.4. @tylerl is there any way to make this configuration permanent for html files in 7.4? – xaph Nov 27 '14 at 14:03
  • `:set smartindent` worked well for me in combination with `1G` `v` `G` `$` `=` (go to first line, set visual mode, go to last line, go to end of last line, smart indent). this selects all the text then indents. – connorbode Oct 16 '15 at 13:53
  • Major caveat: if you run these commands in the vim command line, you need to re-open the file with `:e`. This is not necessary if putting them into `~/.vimrc` – wisbucky Jun 22 '18 at 17:50
  • A tip for people who like `gg=G`, try `gg=G\`\``, then try `:set nnoremap ii gg=G\`\`` . That way, you just press `\ii` and you re-indent your whole file and go back to precisely where you were (you can also use this, for example, with clang-format for c++... `:set equalprg=clang-format` ... etc...) – Nathan Chappell Oct 03 '19 at 13:10
98

With filetype indent on inside my .vimrc, Vim indents HTML files quite nicely.

Simple example with a shiftwidth of 2:

<html>
  <body>
    <p>
    text
    </p>
  </body>
</html>
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • 23
    From http://www.chovy.com/web-development/fix-indentation-and-tabs-in-vim/ found that I needed to reload the file with :e after filetype indent on. – Marc Stober May 17 '12 at 01:06
  • 2
    Note that in `vim 7.4` the default indentation settings will **fail** for this example, as `html`, `body`, and `p` are not indented by default. See [this answer](http://stackoverflow.com/a/19351416/446554). – Cory Klein Jun 09 '14 at 17:05
  • vim default indentation is well. I like this. But when I install – alhelal Apr 03 '18 at 02:47
  • Thanks @DonReba! I had already `set syn=html`, but didn't realise I also needed to `set ft=html` as well. Worked like a charm! – J.M. Janzen Sep 16 '20 at 12:59