把以下的內容加入到vimrc,然後用敲入:PrettyXML即可自動格式化XML。但是這要求xmllint位於你的PATH變數裡。沒有的同學,請安裝包libxml2。
function! DoPrettyXML()
" save the filetype so we can restore it later
let l:origft = &ft
set ft=
" delete the xml header if it exists. This will
" permit us to surround the document with fake tags
" without creating invalid xml.
1s///e
" insert fake tags around the entire document.
" This will permit us to pretty-format excerpts of
" XML that may contain multiple top-level elements.
0put =''
$put =''
silent %!xmllint --format -
" xmllint will insert an header. it's easy enough to delete
" if you don't want it.
" delete the fake tags
2d
$d
" restore the 'normal' indentation, which is one extra level
" too deep due to the extra tags we wrapped around the document.
silent % " back to home
1
" restore the filetype
exe "set ft=" . l:origft
endfunction
command! PrettyXML call DoPrettyXML()
本文編譯自 http://vim.wikia.com/wiki/Pretty-formatting_XML