Mild-Mannered Canadian Fury

Doug Stephen is Politely Peeved

Using TextMate 2's rmate with Sublime Text 2

Tue, 26 Jun 2012 Ā«permalinkĀ»

New Update (24 April 2013)

Combed through this post and updated a few of the links, as the textmate repos moved from avian/textmate to textmate/textmate, so some of the links were broken. This is one of my most frequently visited posts, so I figured I owed it some care.


While TextMate 2 is an OS X application, Sublime Text 2 is cross-platform, running on Windows, OS X, and Linux. Additionally, the rmate utility is nothing more than a Ruby script. The stand-alone script is freely available on GitHub, and it runs just fine on any remote computer that has a working Ruby installation. So even if you don’t use OS X, as long as you’re a Sublime Text 2 user running a system with ssh capabilities and your remote system has Ruby or bash, you should be able to utilize the rsub plugin. That being said, I have little-to-no experience using cygwin or PuTTY or WinSCP for reverse tunneling an ssh session, so I can’t speak for how well this will work on a local Windows machine, but I can’t think of any reason why this wouldn’t work given enough elbow grease.

Sublime Text 2 exited beta today. This isn’t “yet another Sublime Text 2 released” post. But this is about how to use the extremelyl awesome rmate utility for TextMate 2 with Sublime Text 2. rmate is a small Ruby script that you deploy on remote computers that lets you edit text files securely and performantly by using a reverse ssh tunnel. It sounds complicated, but it’s super easy to set up.

After only a few minutes of research in to whether or not this functionality exists for Sublime Text 2, I stumbled on to the rsub package, a Sublime Text 2 plugin that lets rmate communicate with Sublime Text 2.

Set up Ruby and rmate/rsub on your remote machine

The first step is to deploy the script to your remote machine(s). rmate is a Ruby script (though there is also a bash version available), so you should probably have Ruby up and running on your remote machine first. If you don’t, it is available readily in most package managers, but I highly recommend using a Ruby environment manager. I prefer the combination of rbenv and ruby-build1. The last-ditch, I-absolutely-hate-Ruby option is to simply grab the bash version. After you download either the Ruby script or the bash script, you need to send the file over to your remote machine. I stuck mine in /usr/local/bin. I also renamed the binary rsub, which is mostly an aesthetic choice but it makes sense for me because I’ve been using Sublime Text 2 nearly exclusively for months now. Send it over with an FTP client, or take this opportunity to learn how to use scp if you’d like.

Set up the Sublime Text 2 rsub plugin

Once you have Ruby and rmate/rsub set up on your remote machine, you need to install the rsub package in Sublime Text 2 on your local machine. I won’t even bother telling you how to do this manually; if you’re using Sublime Text 2 without Sublime Package Control then you have another project to undertake. Open up the command panel, find Install Package, and search for rsub. Install.

Test out the installation

All the pieces are in place, time to make sure everything works. You’ll need to ssh in to your remote machine while allowing for a reverse tunnel. This is done by passing in the -R option to ssh, and identifying the port on which to tunnel out of on the remote machine, as well as a landing point for the other end of the reverse tunnel. We want the reverse tunnel to end up back on our local machine, and the default port for rmate/rsub is 52698. A good example would be:

ssh -R 52698:localhost:52698

Now test out your rmate/rsub script. Run the script on a text file the same way you would with vim or emacs, and it should tunnel through to your local machine. I like to run rsub -w, to enforce that the command waits for you to close the file before returning. You can see other options by passing in -h.

Configure ssh for easy reverse tunneling

Now that we know everything works, let’s configure ssh to automatically set up a reverse tunnel back to the local machine whenever we connect to our remote box. We can do this using the ssh configuration file. This file sets up options based on blocks for different hosts. Some people recommend using a “wildcard” block to always set up a reverse tunnel, but have individual blocks to only tunnel back on machines where I do a lot of remote editing2. The file you want to edit is ~/.ssh/config. You may need to create this file; it didn’t exist on my OS X Lion installation. Add the following to your config file for the machine you want to tunnel back from:

  RemoteForward 52698 localhost:52698

And now, when you remote in to this machine, you will always tunnel back so you can easily use rmate/rsub.

  1. If you’re on Arch, my distribution of choice, both of these are packaged in the AUR. I use packer to work with the AUR, but I haven’t used any alternatives and I’m on the fence about whether or not I love packer. But if you know how to use the AUR, you can get rbenv and ruby-build really easily. If your remote machines are Macs, both of these are in Homebrew as well.

  2. Yeah, first world problems; I have more than one remote machine I have to fiddle with.