Editing xml/xsl files via sftp/scp in Vim for windows

I have used Vim on linux for around 9 years now, but have never really bothered using it on windows. I recently had to edit a 5.5Mb XML document, and rudely discovered that Eclipse was simply not capable of handling it - even with 1.5GB of RAM allocated to it. So I decided to give Vim for windows a go. You'll be pleased to know that the simple find/replace that I killed after an hour in Eclipse took less than 30 seconds to complete in Vim!

1. Installing and setting up VIM

Download Vim for windows, and install it.

To add support for xml tag automatic closing, download the latest version of the xml.vim plugin, and copy it into the ftpplugin directory (on my machine located at: C:\Program Files\Vim\vim70\ftplugin).

I also modified the default settings file (Edit > Startup Settings) by opening Vim and adding the following to the top of the file and saving it.

set guifont=consolas
set ruler
set number
set wrap
set ai
colors slate
let xml_use_xhtml = 1

The first line should be changed to your favourite editing font that you have installed on your system. The last line (let xml_use_xhtml) is to make the xml.vim plugin use xhtml rather than plain old html.

2. Adding support for XMLLINT

XMLLINT provides formatting and validation support to xml files, and can be called from within Vim.

Download and uncompress the latest versions of the following 3 files:

  • libxml2-*-win32.zip
  • iconv-*-win32.zip
  • zlib-*-win32.zip

Create a new directory, and copy the contents of the the 'bin' directories in each of the three uncompressed directories above into this new directory. Once you have copied the files into your new directory you can delete the original uncompressed directories.

On my machine I named the new directory 'bin' and created it at C:\bin.

Setting your 'Path'

You now need to add that newly created directory to your system's PATH. In Vista (yes I am using Vist 5456) you can do this in the 'System Properties' control panel > 'Advanced' tab > 'Environment Variables...'. I am 99% sure that the process is identical in XP - just to lazy to check.

In the 'System Variables' section, select 'Path', edit it, and add the path to the new directory you created above.

You will need to restart your machine to make sure the new Path settings are loaded.

Once your machine has restarted try opening an xml document in Vim and entering:

:!xmllint --valid --noout %

You should see the results of the validation in a new console window, indicating any errors and what line those errors are on.

To format your xml/xslt document, try:

:%!xmllint --format -

If an error should occur, or the results of the formatting are incorrect you can simply undo the changes.

3. Editing files remotely using sftp/scp

The last step is adding support for remote editing of files via sftp/scp. This is simply a matter of downloading and installing WinSCP and setting Vim as the default editor.

Download the latest version of WinSCP and install it.

Once installed, setup a new connection to your sftp/scp server and in Preference > Editors, add Gvim as an editor and move it to the top of the list (or do as I did, and delete the other editors listed). Make sure 'Multiple Files' is selected under 'Allow multiple remote opened files'.

I also set up WinSCP to use an 'Explorer-like' interface (Preferences > Environment > Interface) as this only then displays the remote folder (and no local folder list).

Click OK to save your changes.

You should now be able to browse your remote server, and double click on any file, which will automatically open in Vim. Any time you save in Vim (:w), WinSCP will save your changes on the remote server automatically.

About

This is the personal blog of Adrian Lynch, owner of Millstream, developers of Spring CMS.

Version: 1.3