Remote Editing for TextMate
This page describes some remote editing scripts I use with the TextMate editor for the Mac.
Note: I've had some comments that the ODBForwarder, and hence these scripts, don't work with Leopard. I hope to look into it, but I'm not sure exactly when I'll get around to it. Until then, people might like to look at Mac FUSE and sshfs.
Download
The remote editing scripts are available here.
Installation
This set of scripts and programs is distributed as a tar file. It can be uncompressed with the command: tar zxf RemEdit.tgz
You should then have the following files inside the unpacked directory:
- ODBForwarder.app
- Remote Editing.tmbundle
- mate
- odbedit
- remoteEdit
- remoteOnEdit
- remoteOpen
You should install these as follows:
- ODBForwarder.app should be copied to your Applications directory. The finder should be opened on the directory after the file is placed there so it can set the correct type signature bindings. Just opening and closing the directory (in the finder, not the shell) is enough. For more information on this program, see http://www.cse.unsw.edu.au/~willu/ODBSuite/
- Remote Editing.tmbundle should be copied to where you are placing your local TextMate bundles. This is probably ~/Library/Application Support/TextMate/Bundles/
- mate should be placed on the remote machine that you want to use TextMate from. You'll probably want to edit this script and replace the willu in the line remoteUser=willu with whatever your user name is on your mac.
- odbedit should be placed in your path. I have this in ~/bin/, but /usr/local/bin/ would be another fine place to put it. More information about this program is here
- remoteEdit, remoteOnEdit and remoteOpen should all be placed in your path. Again, I have these in ~/bin/, but /usr/local/bin/ would be another fine place to put them. remoteOnEdit and remoteOpen are both support scripts that you probably wont need to call directly. remoteEdit is really the only one of these that could be usefully called from the command line directly - for usage see below.
You will also need to run the command:
defaults write org.dyndns.wuther.ODBforwarder ODBEditor com.macromates.textmate
to tell ODBForwarder to use TextMate rather than its default of BBEdit. That command will need to be run before you try any of the scripts. Otherwise you should log out and log back in to restart ODBForwarder.
Usage:
The purpose of these scripts and programs is to allow TextMate to edit files on remote machines. This is achieved by copying the remote files to local temporary storage, letting TextMate edit them, and then copying them back whenever TextMate saves its changes. All the copying is achieved using scp. You'll need to have scp set up with key-based login so that a password is not required to connect from your mac to the remote machine. I highly recommend SSHKeyChain for this.
There are three different methods provided for telling TextMate which remote file to open:
- The mate script sits on your remote system. You can use it as you would any other editor command - mate file. At the moment it is a fairly simple script. It requires the file to already exist and doesn't do much error checking. When you run this script it attempts to connect back to the machine you connected from under the assumption that this is your Mac (it isn't hard to edit the script if this assumption is wrong). You may need to enter your mac password at this stage because the script needs to tell your mac to open the file. The file should then open in TextMate on your mac. The script will block until you close the file on your mac.
- The 'Open remote file...' command in the Remote Editing bundle is the second way of opening a remote file. It will bring up a dialog asking for the scp-style descriptor of the remote file you wish to edit. Again, that file must already exist. It will then be opened locally in TextMate.
- The remoteEdit script sits on your mac. It allows you to edit a file remotely from the Mac command line (if you ever need to). It takes a single argument which is the scp-style descriptor of the remote file you wish to edit. The main purpose of this script is to be the guts of the 'Open remote file...' TextMate bundle command.
Justification:
All of these scripts are a little rough right now - but they are certainly useful. Patches are welcome if you clean any of it up - willu.sp@cse.unsw.edu.au .
The goal is to add something like the sftp functionality in BBEdit to TextMate.
So, why did I make my own system rather than just using Transmit, Cyberduck, or one of the other FTP programs that works with TextMate?
- I didn't want another App sitting open, especially with an open connection to the remote machine. I didn't want an app taking up screen/dock real-estate, and I didn't want a hidden app that I would have to close separately. (It turns out that my method also leaves an App running in the background - ODBForwarder. This is a significantly smaller process than an ftp client though - it is small enough that you can leave it running all the time with no appreciable CPU or Memory hit.)
- I couldn't see an easy way of making the 'mate' script to open files remotely with Cyberduck or Transmit. This probably is possible with osascript but I prefer my way.
- This setup uses the Unix scripting base of OSX and so all the security-sensitive bits, such as scp, get updated by Apple. There is no separate set of updates I need to worry about.
Technical Details
These scripts work as follows. When asked to open a file they create a temporary local directory. The remote file is copied across to that local directory using scp. As well as that local copy of the file for TextMate to edit, a backup copy is made so we can check to see if we accidentally missed any edit events, and some data about the location on the remote machine for the file is stored. TextMate is then asked to edit the local file using the odbedit command (which then uses ODBForwarder.app). The odbedit command will ask any ODB aware editor (of which TextMate and BBedit are both examples) to open a file, and can then call supplied scripts when the file is edited or closed. In this case, the scripts copy the modified file back to the remote machine when it is changed. The local directory is removed when the file is closed in TextMate.