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.

share|improve this question
up vote 72 down vote accepted

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>
share|improve this answer
11  
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. – mmcdole May 2 '09 at 20:40
12  
It works for me. set ft=html<cr>set si<cr>gg=G<cr>. Formats this page quite well. – Don Reba May 2 '09 at 20:51
5  
+1 verified that "filetype indent on/off" switches the magic on or off. – Wim Coenen May 2 '09 at 21:15
4  
Yes, after setting smart indent, filetype=html, and and filetype ident on it worked for me. – mmcdole May 2 '09 at 21:17
15  
From 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 1:06

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.

share|improve this answer
3  
much better concise answer than the more voted for one above... – chohi Mar 24 '13 at 16:12
    
@tyrel, thanks, but for me is not working.. This is the file's content: pastebin.com/gagia8W2 . The file is called home.html. I don't have any problem to indent .php files. Here you have my .vimrc: pastebin.com/FAJ0MCA9 – ziiweb 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 3: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. – Cory Klein Jun 9 '14 at 17:07
    
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

Note that in vim 7.4 the html tags html, head, body, and some others are not indented by default. This makes sense, as nearly all content in an html file falls under those tags. If you really want to, you can get those tags to be indented like so:

:let g:html_indent_inctags = "html,body,head,tbody" 

See here and here for more information.

share|improve this answer
    
I love you.....:) – Leng Sep 24 '15 at 7:43
    
this is the winner in my book! Using Vim 7.4 on Windows and this work fantastically! :D – Michael J Mar 12 '16 at 7:38

The main problem using the smart indentation is that if the XML (or HTML) sits on one line as it may end up coming back from a curl request then gg=G won't do the trick. Instead I have just experienced a good indentation using tidy directly called from VI:

:!tidy -mi -xml -wrap 0 %

This basically tells VI to call tidy to cleanup an XML file not wrapping the lines to make them fit on the default 68 characters wide lines. I processed a large 29MB XML file and it took 5 or 6 seconds. I guess for an HTML file the command should therefore be:

:!tidy -mi -html -wrap 0 %

Hope this could help.

share|improve this answer
    
I don't believe html is needed, as it defaults to html – lsiebert Dec 25 '14 at 4:35
2  
Agreed with you @Isieber. However, I guess it makes it easier to understand the logic and might even be considered good practice by some people. – oscaroscar Mar 6 '15 at 10:38
1  
FYI, tidy chokes if the HTML includes SVG (e.g., charts, etc.). – Alex Quinn Jan 24 at 22:49

This is my solution that works nicely for opening "ugly" HTML in a nicely spaced way:

vim fileIn.html -c "set sw=2 | %s/>/>\r/ | execute 'normal gg=G' | set nohlsearch | g/^\\s*\$/d"

The sw command is because my default is 4, which is too high for HTML.

The next part adds a newline (Vim thinks it's a carriage return, sigh) after each element (>)

Then re-indent the entire file with =

Then unhighlight > (since I have set hlsearch in my vimrc)

Then remove all empty / whitespace-only lines (see here for more, also this is double-escaped because it's in the shell)

You can even add | wq! fileOut.html to the end if you don't want to enter Vim at all, but just clean up the file.

share|improve this answer

I use this script: https://github.com/maksimr/vim-jsbeautify

In the above link you have all the info:

  1. Install
  2. Configure (copy from the first example)
  3. Run :call HtmlBeautify()

Does the job beautifully!

share|improve this answer
2  
note: this requires nodejs and npmjs.org/package/js-beautify – Frederik Deweerdt Sep 5 '14 at 17:10
1  
haha, why would you want to use node + js to clean up html in vim, which has had this capability builtin for longer than node even exists... boggles my mind.. – mb21 Jan 31 at 14:52
    
Builtin vim isn't as good... – SimonW Feb 2 at 22:38

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

share|improve this answer
    
+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. – mmcdole May 2 '09 at 20:45

Here's a heavy-weight solution that gets you indenting, plus all the HTML pretty-printing you don't necessarily want to care about while you're editing.

First, download Tidy. Make sure you add the binary to your path, so you can call it from any location.

Next, create a config file describing your favorite HTML flavor. Documentation is not great for Tidy, but here's an overview, and a list of all the options. Here's my config file:

bare: yes
break-before-br: no
clean: yes
drop-proprietary-attributes: yes
fix-uri: yes
indent-spaces: 4
indent: yes
logical-emphasis: yes
markup: yes
output-xhtml: yes
quiet: yes
quote-marks: yes
replace-color: yes
tab-size: 4
uppercase-tags: no
vertical-space: yes
word-2000: yes
wrap: 0

Save this as tidyrc_html.txt in your ftplugin folder (under vimfiles).

One more file: add the following line to (or create) html.vim, also in ftplugin:

map <leader>tidy :%! tidy -config ~/vimfiles/ftplugin/tidyrc_html.txt <CR>

To use it, just open an HTML file, and type /tidy. (That / is the <leader> key.)

There you go! Not a quick solution, by any means, but now you're a bit better equipped for editing those huge, messed-up HTML files.

share|improve this answer

You can integrate both tidy and html-beautify automatically by installing the plugin vim-autoformat. After that, you can execute whichever formatter is installed with a single keystroke.

share|improve this answer

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.