Vim Tips Wiki
No edit summary
m (Reverted edits by 2003:E2:BBF0:501:40C5:191B:7072:DCCE (talk) to last version by Hwolfe)
 
(13 intermediate revisions by 11 users not shown)
Line 1: Line 1:
  +
{{review}}
<h3>WEB SITE TERMS &amp; CONDITIONS</h3>
 
  +
{{TipImported
<strong>1. USAGE</strong>
 
  +
|id=18
  +
|previous=17
  +
|next=19
  +
|created=2001
  +
|complexity=advanced
  +
|author=scrott
  +
|version=6.0
  +
|rating=50/29
  +
|category1=HTML
  +
|category2=
  +
}}
  +
If you are working with HTML, you can use Vim to clean up the formating of the HTML code. This tips show how to do it.
   
  +
==Using tidy for cleaning up your code==
1.1 The Cubicle Think Tank web site, <strong><a href="http://cubiclethinktank.com/">http://cubiclethinktank.com</a></strong>, is the registered property of Alterpreneur LLC registration number: LC1183412.
 
  +
You need to install [http://tidy.sourceforge.net/ 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 [http://sourceforge.net/projects/jtidy/ Jtidy], a Java implementation of Tidy available. This can also be used for cleaning up your HTML.
   
  +
===Using tidy for html files===
1.2 Any persons accessing and/or using the Cubicle Think Tank web site for any reason whatsoever subject themselves to and agree to the terms and conditions and privacy policy of Alterpreneur LLC when accessing the web site as set out below.
 
  +
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.
  +
<pre>
  +
:vmap ,x :!tidy -q -i --show-errors 0<CR>
  +
</pre>
  +
This means, from visual mode, you can simply press <code>,x</code> and Vim will filter your content through tidy. This will call tidy in quiet mode (<code>-q</code>) and instruct it to indent the lines (<code>-i</code>). Errors won't be shown (<code>--show-errors 0</code>), since the lines should not be lost.
   
  +
Alternatively, you can also create a {{help|prefix=no|:command}} that calls tidy:
1.3 Unless otherwise specified this web site is only intended to provide the user with information regarding Cubicle Think Tank, its products and services.
 
  +
<pre>
  +
:command Thtml :%!tidy -q -i --show-errors 0
  +
:command Txml :%!tidy -q -i --show-errors 0 -xml
  +
</pre>
   
  +
===Automatic formatting of XML files===
1.4 Alterpreneur LLC expressly reserves the right, in its sole and absolute discretion, to alter and/or amend any criteria or information set out in this web site without prior notice.
 
  +
You can also use tidy to format xml files
  +
<pre>
  +
:au FileType xml :%!tidy -i -xml --show-errors 0 2>/dev/null
  +
</pre>
   
  +
This sets up a FileType autocommand, that will clean up your source using tidy, whenever Vim set's the Filetype to xml.
1.5 Nothing on this web site shall be construed as an offer by Alterpreneur LLC to you, the user, but merely an invitation to do business.
 
   
  +
===Using built-in commands===
1.6 Unless explicitly stated, on a per page basis, these terms and conditions apply to all Alterpreneur LLC sites.
 
  +
Using Vim's {{help|prefix=no|'equalprg'}} option, you can use the {{help|prefix=no|id==}} operator to reformat using HTMLTidy. Or, you can use the {{help|prefix=no|'makeprg'}} option to just show the suggestions from HTMLTidy in your {{help|prefix=no|quickfix}} list.
   
  +
<pre>
<strong>2. PRIVACY POLICY</strong>
 
  +
:setlocal equalprg=tidy\ -quiet\ --show-errors\ 0
  +
:setlocal makeprg=tidy\ -quiet\ -e\ %
  +
</pre>
   
  +
At this point you can use <code>make</code> to clean up the full file or you can use <code>=</code> to clean up sections. Vim also ships with a tidy compiler plugin, that set's the {{help|prefix=no|'makeprg'}} automatically for you and also sets the {{help|prefix=no|'errorformat'}} setting for you. To make this work, simply type: <code>:compiler tidy</code>
2.1 Alterpreneur LLC respects your privacy and cares how information about you is used and shared. We appreciate your trust in this regard and assure you that we will make every reasonable effort to manage your information carefully, securely and sensibly. This notice describes our Privacy Policy. When visiting and/or interacting with any Alterpreneur LLC web site, you are expressly consenting to the collection, collation, processing or disclosure of your personal information for the purposes and in the manner described in this Privacy Policy without qualification, so please read it carefully.
 
   
  +
===Setting up tidy using a filetype plugin===
2.2 Information about our customers is an important part of our business. Such information may be collected, collated, processed or disclosed for the purposes set out hereunder. Where we intend using your information for any alternative purpose, we will first obtain your written consent to do so.
 
  +
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 {{help|prefix=no|filetype-plugin}}
2.3 We may compile profiles and reports based on your personal information for statistical purposes or to extend our service offering and may freely trade with such profiles and statistical data with other organizations, specifically within the communities created by our Web sites: provided that such profiles and statistical data are not capable of identifying you as an individual, and that they will not contain any sensitive or confidential information about you. Any information shared by us in this regard will be made available to you on request, so that you may monitor our use of information gathered.
 
   
  +
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 {{help|prefix=no|<buffer>}} argument for mappings and the <code>-buffer</code> argument for commands ({{help|prefix=no|command-buffer}}).
2.4 You hereby consent to us disclosing your information to third parties for the purposes set out below. However we will never disclose sensitive or confidential information about you to any third party, unless required to do so by operation of the law or with your prior written consent, if it pertains to any transaction or activity between you and the third party facilitated by Alterpreneur LLC.
 
   
  +
==References==
2.5 Your information may be disclosed for the following purposes:
 
  +
*{{help|id==}}
  +
*{{help|'equalprg'}}
  +
*{{help|'makeprg'}}
  +
*{{help|'errorformat'}}
   
  +
==Comments==
2.5.1 Agents:
 
  +
If you are using tidy under Windows, you need to set your <code>shellpipe=2></code> 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.
We employ other companies and individuals to perform functions on our behalf. Examples include analyzing data, providing marketing assistance, processing credit card payments, and providing customer service. They have access to personal information needed to perform their functions, but may not use it for any other purpose.
 
  +
----
  +
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.
2.5.2 Business transfers:
 
In the event of the purchase or restructuring of the whole or part of our business, certain intellectual property, including our customer's information, may be transferred to another party in the normal course of business.
 
   
  +
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:
2.5.3 For credit purposes: If we receive, compile, retain or report any account or personal information pertaining to you or your financial position, we will protect the confidentiality of that information, and report or release that information only to you, or to another person to the extent permitted or required by applicable law.
 
   
  +
setlocal makeprg=tidy\ -quiet\ -errors\ %
You have the right:
 
  +
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.
1. to be compensated by us, should we have supplied inaccurate information, for the cost of correcting it.
 
   
  +
----
Where you wish to challenge any such information, you may notify the relevant credit bureau within a reasonable time, and the credit bureau will then take reasonable steps to seek credible evidence in support of the challenged information and provide you with a copy of such evidence OR remove the information from its records if it is unable to find such evidence.
 
  +
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
2.5.4 Where you consent: Other than as set out above, you will receive notice when information about you will be used for some other purposes and/or where it might be disclosed to third parties, and you will have an opportunity to choose not to share the information by notifying us accordingly.
 
   
  +
the -i indents, that is optional
2.6 Alterpreneur LLC gathers the following information via its web sites:
 
   
  +
The rest of the tidy options can be found here:
2.6.1 Information provided by you: We receive and store any information you enter on our web site or provided by you in any other way. You may choose not to provide certain information, but then you might not be able to take advantage of the many features on our web sites. We use the information that you provide to continually improve our service offering to you by analyzing the information and corresponding trends and the like that may be found. The quality of any analysis or activity based on information is of course dependent on the integrity of the information supplied. Alterpreneur LLC reserves the right to verify any information provided by you.
 
  +
http://tidy.sourceforge.net/docs/quickref.html
   
  +
----
2.6.2 Information automatically gathered: We receive and store certain types of information whenever you interact with the web site. For example, like many web sites, we use "cookies", and we obtain certain types of information when your web browser accesses our web sites. The "cookies" often transfer security related information, so this facility must be activated on your browser whilst accessing our web sites or they may malfunction.
 
  +
Call a function for tidy - add to your vimrc
  +
<pre>
  +
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
  +
</pre>
   
  +
----
2.6.3 E-mail communications: To help us make e-mails more useful and interesting, we often receive a confirmation when you open an e-mail from us. We also compare our customer list to lists received from other companies, in an effort to avoid sending unnecessary messages to our customers or to grow our customer base.
 
  +
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:
  +
<pre>
  +
map <F12> :%!tidy -q --tidy-mark 0 2>/dev/null<CR>
  +
</pre>
   
  +
----
2.6.4 Information from other sources: To improve our service offering, we might receive information about you from other sources and add it to our information. The more information we have, the more we can tailor our service offering to meet market expectations.
 
  +
I use this:
 
  +
<pre>
<strong>3. ELECTRONIC COMMUNICATION</strong>
 
  +
command Txml set ft=xml | execute "%!tidy -q -i -xml"
 
  +
command Thtml set ft=html | execute "%!tidy -q -i -html"
Alterpreneur LLC will communicate with you by email or by posting notices on this web site and you agree that all agreements, notices, disclosures and other communications that we provide to you electronically from time to time shall satisfy any legal requirement that such communications be "in writing".
 
  +
</pre>
 
  +
You can undo the formatting, but the ft change won't be undone.
<strong>4. TERMS OF AGREEMENT</strong>
 
  +
----
 
4.1. <strong>RATIFICATION</strong>:
 
You agree that by visiting or using Alterpreneur.com or any other Alterpreneur LLC web site that you are authorized to bind yourself or the legal entity you represent and do so bind yourself or the legal entity you represent to this Agreement, by any national and/or international conventions.
 
 
4.2. <strong>LEGAL ENTITY</strong>:
 
Any account registered on this web site must be held in your name or the name of a legal entity you represent. In the event that the account is registered in the name of a natural person, the person must provide his or her national identity, social security or like number if required to do so. If the account is to be held in the name of a juristic person, the entity must provide its unique registration or like number.
 
 
4.3. <strong>SERVICES</strong>:
 
You acknowledge that you have the necessary knowledge and infrastructure to use the Alterpreneur LLC web site and services. You further acknowledge that Alterpreneur LLC is not required to provide any information or other services not specifically related to the Alterpreneur LLC web site and services.
 
 
4.4. <strong>LAWFUL PURPOSE</strong>:
 
You may only use the Alterpreneur LLC web sites and the services pertaining thereto for lawful purposes as defined by the legislation of the United States and international conventions. Where a conflict between such laws and regulations exist, the laws and regulations of the United States shall prevail. Use and transmission of any material in violation of any international, national or local regulation or law is prohibited. This includes, but is not limited to intellectual property infringement, publishing material legally judged to be defamatory, threatening or obscene, pornography or information protected by trade secrets.
 
 
4.5. <strong>ALTERNATIVE DISPUTE RESOLUTION</strong>:
 
 
4.5.1 Should any dispute arise out of or in connection with this agreement, either party will be entitled to require, by written notice to the other, that the dispute be referred to arbitration in terms of this clause.
 
 
4.5.2 Subject to the provisions of this clause, an arbitration will be held under the provisions of the Federal Arbitration Act of the United States, as amended from time to time, provided that:
 
 
4.5.2.1 the arbitration will be held at a venue and in accordance with the procedures determined by the arbitrator and may be held in an informal and summary manner, on the basis that it will not be necessary to observe or carry out the usual formalities relating to procedure, pleadings and discovery, or the strict rules of evidence.
 
 
4.5.2.2 the arbitrator shall be entitled:-
 
 
a) to investigate or cause to be investigated any matter, fact or thing which he considers necessary or desirable in connection with the dispute and for that purpose he shall have the widest power of investigating all the books and records of both parties to the dispute and the right to take copies or make extracts therefrom and the right to have them produced or delivered at any reasonable place required by him for the aforesaid purpose;
 
 
b) to interview and question under oath any of the parties;
 
 
c) to decide the dispute according to what he considers just and equitable in the circumstances;
 
 
d) to make such award, including an award for specific performance, an interdict, damages or a penalty or otherwise, as he, in his discretion, may deem fit and appropriate;
 
 
e) the arbitration shall be held as quickly as possible after it is demanded with a view to its being completed within 30 (thirty) days after it has been so demanded.
 
 
4.5.2.3 Any award that may be made by the arbitrator shall be final and binding; will be carried into effect; and may be made an order of any court to whose jurisdiction the parties to the dispute are subject.
 
 
<strong>5. COMMUNICATIONS AND OTHER CONTENT</strong>
 
 
5.1 You may send content and other communications to and/or via this website provided that the content is not illegal, obscene, objectionable, threatening, defamatory, invasive of privacy, infringing of intellectual property rights, or otherwise injurious to third parties and does not consist of or contain software viruses, political campaigning, commercial solicitation, chain letters, mass mailings, or any form of "spam." You may not use a false email address, impersonate any person or entity, or otherwise mislead as to the origin of a communication. Alterpreneur LLC reserves the right to remove or edit such content in its sole discretion.
 
 
5.2 You hereby represent and warrant that you own or otherwise control all of the rights to the content that you post on this web site; that the content is accurate; that use of the content you supply does not violate this policy and will not cause injury to any person or entity; and that you indemnify Alterpreneur LLC or its affiliates from all and any claims resulting from content you supply to Alterpreneur LLC. Alterpreneur LLC shall not be held responsible or liable for any content posted on the Alterpreneur LLC web sites.
 
 
<strong>6. THIRD PARTY LINKS</strong>
 
 
Parties other than Alterpreneur LLC and its subsidiaries may provide content on this web site form time-to-time. In addition, we provide links to the web sites of affiliated and certain other organizations. Alterpreneur LLC will not be responsible for examining or evaluating the content of, and we do not warrant the offerings of any of these organizations or their Web sites. Alterpreneur LLC does not assume any responsibility or liability for the actions, product, and content of these and any other third party Web sites. You should carefully review their privacy statements and other conditions of use.
 
 
<strong>7. DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY</strong>
 
 
7.1 Alterpreneur LLC shall not be liable for any damage, loss or liability of whatsoever nature arising from the use or inability to use this web site or the services or content provided from and through this web site. Furthermore, Alterpreneur LLC makes no representations or warranties, implied or otherwise, that, amongst others, the content and technology available from this web site are free from errors or omissions or that the service will be 100% uninterrupted and error free.
 
 
7.2 This web site is supplied on an "as is" basis and has not been compiled or supplied to meet the user's individual requirements. It is the sole responsibility of the user to satisfy itself prior to entering into this agreement with Alterpreneur LLC that the service available from and through this web site will meet the user's individual requirements and be compatible with the user's hardware and/or software.
 
 
7.3 Information, ideas and opinions expressed on this site should not be regarded as professional advice or the official opinion of Alterpreneur LLC and users are encouraged to consult professional advice before taking any course of action related to information, ideas or opinions expressed on this site.
 
 
7.4 All items purchased from Alterpreneur LLC are made pursuant to agreements with shipping and delivery agents and risk of loss pass from Alterpreneur LLC to such agents upon delivery of any item to such carrier.
 
 
7.5 Neither Alterpreneur LLC nor any of its agents or representatives shall be liable for any damage, loss or liability of whatsoever nature arising from the use or inability to use any product sold on this web site.
 
 
<strong>8. GOVERNING LAW</strong>
 
 
This web site is hosted, controlled and operated from the United States, and thus the United States Law governs the use or liability to use this web site and these terms and conditions.
 
 
<strong>9. NON-WAIVER</strong>
 
 
The failure of Alterpreneur LLC to exercise or enforce any right or provision of these terms shall not constitute a waiver of such right or provision.
 
 
<strong>10. VARIATION AND SEVERABILITY</strong>
 
 
10.1 Alterpreneur LLC reserves the right to amend or make changes to this web site, its policies, and this Agreement at any time. You are required to visit this web site regularly in order to acquaint yourself with any changes or amendments made to this web site, its policies and this Agreement.
 
 
10.2 No variation to this Agreement shall be enforceable unless such variation is published on this web site by Alterpreneur LLC.
 
 
10.3 In the event that any clause in this Agreement is deemed to be invalid, void, or for any reason unenforceable, that clause shall be deemed severable and shall not affect the validity and/or enforceability of any remaining term or condition.
 
 
<strong>11. INFORMATION SECURITY</strong>
 
 
11.1 We protect the security of your information during transmission by using Secure Sockets Layer (SSL) software, which encrypts information you provide. Once stored, the information is encrypted at column level within our database.
 
 
11.2 It is very important for you to prevent unauthorized access to your password and to your computer. Be sure to sign off when finished using a shared computer and delete your browsing history as well as all cookies and passwords which might be saved.
 
 
<strong>12. CONDITIONS OF USE, NOTICES, AND REVISIONS</strong>
 
 
If you choose to visit any Alterpreneur LLC web site, your visit and any dispute over privacy is subject to this Privacy Notice and our Conditions of Use. If you have any concern about privacy at any Alterpreneur LLC Web site, please send us a thorough description of your concern to info@alterpreneur.com and we will try to resolve it. Use of information that we gather now is subject to the Privacy Notice in effect at the time of use of such information. We may e-mail periodic reminders of our notices and conditions, unless you have instructed us not to do so, but you should check our web site frequently to acquaint yourself with recent changes and/or amendments.
 
 
<strong>16. CONTACT INFORMATION</strong>
 
 
If you have any questions, queries or wish to request permission to use any part of this web site, including, linking, framing, or searching, please contact us at:
 
 
<strong>Street Address</strong>:
 
Alterpreneur LLC
 
8821 Forest Heights Dr.
 
Saint Louis
 
MO
 
63123
 
 
<strong>Postal Address</strong>:
 
Alterpreneur LLC
 
55 Grasso Plz
 
PO BOX 20197
 
Saint Louis
 
MO
 
63123
 

Latest revision as of 06:30, 26 February 2018

Tip 18 Printable Monobook Previous Next

created 2001 · complexity advanced · author scrott · version 6.0


If you are working with HTML, you can use Vim to clean up the formating of the HTML code. This tips show how to do it.

Using tidy for cleaning up your code[]

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.

Using tidy for html files[]

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.

Alternatively, you can also create a :command that calls tidy:

:command Thtml :%!tidy -q -i --show-errors 0
:command Txml  :%!tidy -q -i --show-errors 0 -xml

Automatic formatting of XML files[]

You can also use tidy to format xml files

:au FileType xml :%!tidy -i -xml --show-errors 0 2>/dev/null

This sets up a FileType autocommand, that will clean up your source using tidy, whenever Vim set's the Filetype to xml.

Using built-in commands[]

Using Vim's 'equalprg' option, you can use the = operator to reformat using HTMLTidy. Or, you can use the 'makeprg' option to just show the suggestions from HTMLTidy in your quickfix list.

:setlocal equalprg=tidy\ -quiet\ --show-errors\ 0
:setlocal  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. 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

Setting up tidy using a filetype plugin[]

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

References[]

Comments[]

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:

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.