Tuesday, January 6, 2009

Vim, Tidy and XML - how to make them work together

Background

I use vim as my main IDE. I found that I often have to muck and view XML. Often this XML is from debugging logs or output that don't add whitespace (and why would they).

The following is my setup for a small vim filetype mapping to automatically clean up xml using HTML tidy. Another great tool.

How To

First, You'll need $HOME/.vim and $HOME/.vim/ftplugin directories.

Create them, if they don't exist:
  mkdir -p $HOME/.vim/ftplugin
Second, create a new ftplugin for xml.
   vim $HOME/.vim/ftplugin/xml.vim
Third, add mapping for tidy (I use F3)
" tidy for xml -q makes it quiet
map <F3> :%! tidy -q -i -xml % <CR>
Now when you edit a file with a type of xml, you can press F3 your xml will be tidy'd. This does require that you save the xml file somewhere first (you'll need a valid filename).

Caveats

  • I've only used this on UN*Xs OSs
  • I use vim 7.x, it will probably work on 6
  • I enable filetype support in my .vimrc with command filetype plugin indent on
  • On solaris I have to do some crazy function key mapping inside of .vimrc
End transmission :)

No comments:

Post a Comment