Contents

You need to install html tidy on your system first. Tidy is a tool to fix invalid HTML content and improve the layout of the resulting markup. There is also Jtidy, a Java implementation of Tidy available. This can also be used for cleaning up your HTML.

When you have tidy for your platform installed and it is available from your path, you can simply set up a mapping to filter your content through it.

:vmap ,x :!tidy -q -i --show-errors 0<CR>

This means, from visual mode, you can simply press ,x and Vim will filter your content through tidy. This will call tidy in quiet mode (-q) and instruct it to indent the lines (-i). Errors won't be shown (--show-errors 0), since the lines should not be lost.

At this point you can use make to clean up the full file or you can use = to clean up sections. Vim also ships with a tidy compiler plugin, that set's the 'makeprg' automatically for you and also sets the 'errorformat' setting for you. To make this work, simply type: :compiler tidy

All those options, mappings and commands can be set up automatically for html/xml files automatically, if you use filetype plugins.

To make this work, simply put your settings into a file called html.vim (use xml.vim for the xml filetype and don't forget the -xml switch for tidy) and place it into the directory ~/.vim/ftplugin/ (Unix) or $VIM/vimfiles/ftplugin (windows, where $VIM is the installtion diretory of Vim). See also filetype-plugin

If you set up commands and mappings using filetype plugins, you should make those buffer-local (e.g. only available for buffers of that filetype. Use the <buffer> argument for mappings and the -buffer argument for commands (command-buffer).

If you are using tidy under Windows, you need to set your shellpipe=2> or else Vim won't see the output from tidy. Apparently these Unix tools write output to stderr instead of stdout and Vim isn't configured by default to handle this situation.

vim indents html very well when I put the line

filetype plugin indent on

into my personal ~/.vimrc (or ~\_vimrc) file. I also think that html-tidy is not able to indent only parts of a HTML file. Therefore, I do not use it as equalprg.

I use html-tidy only in order to check if my HTML document is well formed. Therefore, I create a ~/.vim/after/ftplugin/html.vim (or ~\vimfiles\after\ftplugin\html.vim or an html.vim placed in the directory that appears last when typing :set runtimepath?) and put into it (among other things) the lines: