sponsor Vim development Vim logo Vim Book Ad

advanced Tip #18: Cleanup your HTML

 tip karma   Rating 51/28, Viewed by 6274 

created:   February 25, 2001 19:59      complexity:   advanced
author:   [email protected]      as of Vim:   5.7

From Johannes Zellner on the vim list:

You can use vim's makeprg and equalprg to clean up HTML. First download html tidy from http://www.w3.org/People/Raggett/tidy/. Then use the following commands.

vim6?
exe 'setlocal equalprg=tidy -quiet -f '.&errorfile;
setlocal makeprg=tidy -quiet -e %

vim5?
exe 'set equalprg=tidy -quiet -f '.&errorfile;
set makeprg=tidy -quiet -e %

At this point you can use make to clean up the full file or you can use = to clean up sections.

:help =
:help equalprg
:help makeprg

 rate this tip  Life Changing Helpful Unfulfilling 

<<Erasing previosuly entered characters in insert mode | line numbers... >>

Additional Notes

[email protected], May 2, 2001 23:05
There is also Jtidy (Java implementation of tidy):

http://sourceforge.net/projects/jtidy/
[email protected], January 11, 2002 5:01
Vim 6.0 comes with a Tidy compiler plugin for use in quickfix mode - $VIMRUNTIME/compiler/tidy.vim
[email protected], April 23, 2002 19:11
Any hints or tips on how to use the tidy.vim plugin?
[email protected], July 4, 2002 12:02
The current version of C Tidy is now at http://tidy.sourceforge.net/  ;
[email protected], July 4, 2002 12:03
Make that http://tidy.sourceforge.net/
[email protected], August 15, 2002 6:25
To use the included compiler script, run `:compiler tidy`. For more info, see :h quickfix and :h compiler-select.

If you are using tidy.vim under WinNT (not sure about 9x), 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.
[email protected], April 29, 2004 21:07
just start with vim
using 6.2 (mandrake 10 .rpm) need add backslaces like in the tidy.vim file
don't know but for me works just with the command:
:setlocal makeprg=tidy\ -quiet\ -m\ -utf8\ %
and then:
:make
[email protected], March 27, 2005 5:00
vim 6.3 intends 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:

setlocal makeprg=tidy\ -quiet\ -errors\ %
setlocal errorformat=line\ %l\ column\ %v\ -\ %m

I have found that the errorformat option must be adapted as shown in order to be able jump through the error list by means of :cn and :cp etc.
Anonymous, March 21, 2006 11:19
Tidy can be used for just a portion of the document by using the --show-body-only flag. For instance, on using vim6 on OSX the above command could be rewritten as:

:exe 'setlocal equalprg=tidy\ -quiet\ -i\ --show-body-only\ true\ -f\ '.&errorfile;

the -i indents, that is optional

The rest of the tidy options can be found here:
http://cvs.sourceforge.net/viewcvs.py/tidy/tidy/htmldoc/Attic/quickref.html?rev=1.15
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo