(move dodgy template back to top, rephrase beginning of existing tip content to emphasize why you'd use it) |
|||
Line 12: | Line 12: | ||
|category2= |
|category2= |
||
}} |
}} |
||
+ | |||
⚫ | |||
+ | |||
==Easy Steps for Beginners (windows)== |
==Easy Steps for Beginners (windows)== |
||
− | + | #Download and install [http://tidy.sourceforge.net/ html tidy] |
|
− | + | #Add this to your vimrc: |
|
+ | <pre> |
||
− | <code><br/> |
||
− | " HTML Tidy, http://tidy.sourceforge.net/ |
+ | " HTML Tidy, http://tidy.sourceforge.net/ |
− | " select xml text to format and hit ,x |
+ | " select xml text to format and hit ,x |
− | vmap ,x :!tidy -q -i -xml<CR |
+ | vmap ,x :!tidy -q -i -xml<CR> |
− | </ |
+ | </pre> |
− | + | #Restart vim, open the html file, select all (or just a region) |
|
− | + | #When the text is selected, type the following (<ENTER> = Hit the Enter key} |
|
+ | <pre> |
||
− | <code><br/> |
||
− | ,x<ENTER |
+ | ,x<ENTER> |
− | </ |
+ | </pre> |
− | + | #HTML should be formatted |
|
− | </ol> |
||
===Automatic formatting of XML files=== |
===Automatic formatting of XML files=== |
||
Line 37: | Line 39: | ||
au FileType xml exe ":silent 1,$!tidy -q -i -xml" |
au FileType xml exe ":silent 1,$!tidy -q -i -xml" |
||
+ | ==Using built-in commands== |
||
− | <br/><br/> |
||
+ | 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. |
||
− | |||
⚫ | |||
− | + | First download [http://tidy.sourceforge.net/ html tidy], then use the following commands. |
|
For vim6 or higher: |
For vim6 or higher: |
Revision as of 15:35, 25 July 2011
created 2001 · complexity advanced · author scrott · version 5.7
Easy Steps for Beginners (windows)
- Download and install html tidy
- 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>
- Restart vim, open the html file, select all (or just a region)
- When the text is selected, type the following (<ENTER> = Hit the Enter key}
,x<ENTER>
- HTML should be formatted
Automatic formatting of XML files
I altered my .vimrc as follows:
au FileType xml exe ":silent 1,$!tidy --input-xml true --indent yes 2>/dev/null"
Here's how you use tidy with xml on Windows:
au FileType xml exe ":silent 1,$!tidy -q -i -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.
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.