Vim Tips Wiki
Register
(Fix typos)
(Remove html character entities)
Line 15: Line 15:
   
 
For vim6:
 
For vim6:
exe 'setlocal equalprg=tidy -quiet -f '.&errorfile
+
exe 'setlocal equalprg=tidy -quiet -f '.&errorfile
 
setlocal makeprg=tidy -quiet -e %
 
setlocal makeprg=tidy -quiet -e %
   
 
For vim5:
 
For vim5:
exe 'set equalprg=tidy -quiet -f '.&errorfile
+
exe 'set equalprg=tidy -quiet -f '.&errorfile
 
set makeprg=tidy -quiet -e %
 
set makeprg=tidy -quiet -e %
   
Line 41: Line 41:
 
To use the included compiler script, run <tt>:compiler tidy</tt>. For more info, see {{help|quickfix}} and {{help|compiler-select}}.
 
To use the included compiler script, run <tt>:compiler tidy</tt>. For more info, see {{help|quickfix}} and {{help|compiler-select}}.
   
If you are using tidy.vim under WinNT (not sure about 9x), you need to set your shellpipe=2&gt; 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.
+
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.
   
 
----
 
----
Line 67: Line 67:
 
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:
 
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\ '.&amp;errorfile
+
:exe 'setlocal equalprg=tidy\ -quiet\ -i\ --show-body-only\ true\ -f\ '.&errorfile
   
 
the -i indents, that is optional
 
the -i indents, that is optional

Revision as of 08:18, 28 September 2008

Tip 18 Printable Monobook Previous Next

created February 25, 2001 · complexity advanced · author scrott · version 5.7


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.

For vim6:

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

For 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.

References

Comments

There is also Jtidy (Java implementation of tidy): http://sourceforge.net/projects/jtidy/


Vim 6.0 comes with a Tidy compiler plugin for use in quickfix mode - $VIMRUNTIME/compiler/tidy.vim


The current version of C Tidy is now at http://tidy.sourceforge.net/


To use the included compiler script, run :compiler tidy. For more info, see :help quickfix and :help 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.


just start with vim using 6.2 (mandrake 10 .rpm) need add backslashes 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

vim 6.3 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:

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.


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


Call a function for tidy - add to your vimrc

command Td :call Tidy()
function Tidy()
  let filename=expand("%:p") " escapes for bash
  let filename=substitute(filename, " ", "\\\\ ", "g")
  let filename=substitute(filename, "(", "\\\\(", "g")
  let filename=substitute(filename, ")", "\\\\)", "g")
  let filename=substitute(filename, "[", "\\\\[", "g")
  let filename=substitute(filename, "]", "\\\\]", "g")
  let filename=substitute(filename, "&", "\\\\&", "g")
  let filename=substitute(filename, "!", "\\\\!", "g")
  let filename=substitute(filename, ",", "\\\\,", "g")
  let filename=substitute(filename, "'", "?", "g")
  let filename2=substitute(filename, ".*", "&.tidy.htm", "")
  let filename3=substitute(filename, ".*", "&.errors.tidy.txt", "")
  execute "!tidy "."-f ".filename3." ".filename." > ".filename2.""
endfunction