Comparing remote files with Vim

I've been using Vim for quite a few years now. One of the nice things about Vim is your often run in to useful features you didn't know existed.

Editing remote files

One feature I recently stumbled over is the ability to edit remote files using scp. To open a remote file you can just pass Vim a URL, for example:

vim scp://remote.example.com//tmp/somefile.txt

Behind the scenes Vim will then copy the file to /tmp, and changes made to this file will be copied back to the remote host via scp.

Instead of opening a remote file while launching Vim, files can also be opened afterwards with the following:

:e scp://remote.example.com//tmp/somefile.txt

In a similar vein, remote files can be read into the current buffer with :r:

:r scp://remote.example.com//etc/hosts

Other protocols

As well as scp, the netrw plugin also supports several other protocols including ftp, rsync and sftp.

Comparing remote files

Normally files are compared in Vim using the diff feature. The easiest way to do this is with the vimdiff command, for example:

vimdiff /etc/krb5.conf /etc/krb5.conf.backup

This can also be used to compare a remote file:

vimdiff /etc/krb5.conf scp://remote.example.com//etc/krb5.conf

The resulting Vim windows will look similar to the following:

vimdiff comparing a local and a remote file

Further reading

If you need more information it's worth referring to the following Vim docs:

  • diff.txt: Vim diff mode documentation
  • pi_netrw.txt: reading and writing files over a network
  • windows.txt: commands for using multiple windows and buffers

Note: the documentation above can also be accessed in Vim directly. For example :help diff.txt will bring up the documentation on the diff feature.