Editing files on a remote server with Sublime Text 2

I've always hated editing files on a remote machine over SSH. I'm not a huge fan of vim, and nano is terrible for anything remotely (hah, get it?) complex.

Being able to use your local editor with all your plugins and everything all set up would be ideal. However, each solution tends to have its caveats.

SFTP

You can use SFTP to access the server with an app like Transmit and then editing the file.

Pros

  • Easiest solution
  • Able to browse directory tree

Cons

  • Generally limited to your user - editing root-owned files requires logging in as root
  • Non-ideal workflow - switch out of the shell to navigate to the file in the app

SSHFS/FUSE

You can mount a server over SSH with something like FUSE for OS X, then editing the file by navigating the mounted filesystem.

Pros

  • Able to browse directory tree
  • Fairly easy to set up

Cons

  • Generally limited to your user - editing root-owned files is difficult
  • Can be slow - Finder tends to enumerate metadata you don't care about
  • Non-ideal workflow - switch out of the shell to navigate to the file in the app

rsub

rsub is a package for Sublime Text 2 which is a port of rmate functionality in Textmate 2. It utilises a script installed on the remote machine which communicates with your local machine over a remote forwarded port.

Pros

  • Ideal workflow - initiate file editing from shell
  • Usable with sudo - sudo rsub /etc/nginx/conf/nginx.conf works!

Cons

  • More effort to set up - rsub needs to be installed on each machine
  • Not ideal for servers where untrusted users have access

Setting up rsub

Please note that the rsub package only supports Sublime Text 2 at this point.

  1. Install rsub with Package Control
  2. Set up ssh port forwarding:
    • You need to set up a remote forward on port 52698 (by default)
    • You can do it every time you log in:
      
      ssh username@server -R 52698:localhost:52698
      
      
    • Or you can configure .ssh/config:
      # You should only do this for servers that you only have
      # access to as anyone on the server can connect to your machine
      Host *.my-company.com
        RemoteForward 52698 127.0.0.1:52698
      
  3. Install the rsub remote script on your server:
    # aurora's fork of rmate is a bash script that only works with Linux servers.
    # See https://github.com/textmate/rmate for a ruby version
    sudo wget -O /usr/local/bin/rsub https://raw.github.com/aurora/rmate/master/rmate
    sudo chmod +x /usr/local/bin/rsub
    
  4. Use it!
    sudo rsub /etc/nginx/conf/nginx.conf