Vim Tips Wiki
m (Reverted edits by 151.33.195.210 (talk | block) to last version by Fritzophrenic)
No edit summary
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
  +
==Easy Steps for Beginners (windows)==
  +
<ol><li/>Download and install [http://tidy.sourceforge.net/ html tidy]
  +
<li/>Add this to your vimrc:
  +
<code><br/>
  +
" HTML Tidy, http://tidy.sourceforge.net/ <br/>
  +
" select xml text to format and hit ,x <br/>
  +
vmap ,x :!tidy -q -i -xml<CR> <br/>
  +
</code>
  +
<li/> Restart vim, open the html file, select all (or just a region)
  +
<li/> When the text is selected, type the following (<ENTER> = Hit the Enter key}
  +
,x<ENTER>
  +
<li/> HTML should be formatted
  +
</ol>
 
{{dodgy|it may be better to use the built-in compiler plugin for the tidy program; see the [[#Comments|comments]]}}
 
{{dodgy|it may be better to use the built-in compiler plugin for the tidy program; see the [[#Comments|comments]]}}
   

Revision as of 06:28, 25 July 2011

Tip 18 Printable Monobook Previous Next

created 2001 · complexity advanced · author scrott · version 5.7


Easy Steps for Beginners (windows)

  1. Download and install html tidy
  2. Add this to your vimrc:
    " HTML Tidy, http://tidy.sourceforge.net/
    " select xml text to format and hit ,x
    vmap ,x :!tidy -q -i -xml<CR>
  3. Restart vim, open the html file, select all (or just a region)
  4. When the text is selected, type the following (<ENTER> = Hit the Enter key} ,x<ENTER>
  5. HTML should be formatted

You can use Vim's makeprg and equalprg to clean up HTML. First download html tidy, then use the following commands.

For vim6 or higher:

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


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


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


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://tidy.sourceforge.net/docs/quickref.html


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

Here is a mapping so Vim calls Tidy when pressing F12. Advantage of this solution: you can undo changes very easily. Put this in your vimrc:

map <F12> :%!tidy -q --tidy-mark 0 2>/dev/null<CR>

I use this:

command Txml set ft=xml | execute "%!tidy -q -i -xml"
command Thtml set ft=html | execute "%!tidy -q -i -html"

You can undo the formatting, but the ft change won't be undone.