1

I do most of my work in vim. I much prefer to edit files using vim locally rather than sshing into a server and running vim there, for many reasons, one of the most important being UI responsiveness on a slow connection.

I know that I can edit remote files using vim and scp and I also have a setup with sshfs that allows me to quite simply cd to a mount point and vim up some files.

However, it really bothers me that every time I save the file, it writes the whole file to the remote server. This really slows down my workflow.

What I really would like to do is have some sort of setup like this:

  1. When opening a remote file with local vim, pull the file's contents and a hash of the file
  2. When saving the file, check if the remote hash has not changed, and if it has not, upload a diff and patch the remote file with the new changes.
  3. Preferably do this all through a persistent ssh connection, so that no time is lost opening a new connection each time.

This would be much faster and much more bandwidth efficient, and have the same functionality.

I usually use git or svn to achieve a similar result, but I really want this kind of minimal functionality for situations when I do not have the luxury of using such SCM systems.

My questions are:

1. Does anyone know of any existing scripts or programs that will do some or all of this for me?

2. Anyone care to whip up some bash one liners that will do the trick? I know it should be able to be done with a few of these...

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
Mike
  • 425
  • 1
  • 4
  • 11
  • I'm curious what kinds of files or links to you have that the typical config (assuming less than a few KiB) file slows you down that much when written completely vs. just writing the new parts? – serverhorror Jun 12 '11 at 10:19

2 Answers2

1

xdelta could do the patching and the hash check for you.

Here is a go, assuming you have the file mounted with sshfs. I kept the file extension in case you want to enjoy syntax highlighting:

#!/bin/bash

if [ ! -f "$1" -o ! -w "$1" ]
then
        echo "ERROR: cannot open $1 for editing"
        exit 1
fi

TMP="/tmp/$(date +%s)-$$-$(basename $1)"
TMP2="$TMP.2"
PAT="$TMP.patch"

/bin/cp $1 $TMP
/bin/cp $TMP $TMP2
/usr/bin/vim $TMP2
/usr/bin/xdelta delta $TMP $TMP2 $PAT
/bin/cp $1 $1.orig

if /usr/bin/xdelta patch $PAT $1.orig $1
then
        /bin/rm $TMP $TMP2 $PAT
else
        echo "ERROR: xdelta patch failed, keeping temporary files $TMP $TMP2 $PAT"
        exit 1
fi

Let's say you call it deltavim or something, you just

deltavim /path/to/remotefile.conf

This will need two copies locally to make the delta, but removes all that if successful. It leaves the original remote file with a .orig, but you can add a line to delete that after the patch if you'd like.

If the remote file changes while you're editing, xdelta will notice that and refuse to patch.

Anyway, maybe that's a starting point for you.

Good luck! (...and never point a loaded, untested script you get off the internet at a file you love...)

Cakemox
  • 25,209
  • 6
  • 44
  • 67
  • Also: I am not sure the patch won't require pulling the entire file down when run using sshfs, so you might have to push the delta to the remote server then call xdelta via ssh to ensure you aren't re-downloading the file and then uploading the full file. Might want to test it both ways. – Cakemox Jan 21 '11 at 23:06
1

When you remotely connect with vim, does it actually write the whole file?

vim ftp://myserver//path/file.txt
Kevin Cantu
  • 111
  • 2