在vim中如何使用tidy为html验证并格式化内容

在ubuntu中安装tidy只要输入命令:

 sudo apt-get install tidy man tidy

查看手册,可以看到比较有用的参数设置,一般我的文件都是utf8编码的,所以tidy命令会跟上-utf8,并且一般情况就是将tidy输出的文件直接替换掉原来的文件,所以-m选项也很有用,输出的HTML也希望是格式化,有缩进的代码,这时就要加上-i参数,这样在命令行上我的tidy命令会如下所示:

 tidy -m -i -utf8 target-files1 target-files2

可以将常用的参数设置写在一个配置文件中,在调用tidy命令时指定-config config-file-name,下面这份配置就是官网上的一个配置例子,稍做了点调整:

// sample config file for HTML tidyindent: autoindent-spaces: 4wrap: 120markup: yesoutput-xml: noinput-xml: noshow-warnings: yesnumeric-entities: yesquote-marks: yesquote-nbsp: yesquote-ampersand: nobreak-before-br: nouppercase-tags: nouppercase-attributes: nochar-encoding: utf8new-inline-tags: cfif, cfelse, math, mroot,mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,munder, mover, mmultiscripts, msup, msub, mtext,mprescripts, mtable, mtr, mtd, mthnew-blocklevel-tags: cfoutput, cfquerynew-empty-tags: cfelse

在vim中,如果需要对当前文件进行html格式化操作,那么可以在vim命令模式下输入以下指令:

:!tidy -m -i -utf8 %

References

  1. Clean up your Web pages with HTML TIDY