Edit remote server files using Sublime Text

Ji Huang 2018-02-05 4 min read

Today I was saw a [post] in 2014 naming “Sublime Text, Putty, and You” that connectingn Putty with Sublime Text so that you can edit remote server files using Sublime text. This is a great method since I’m always struggling with Vim commands. I tried several times to adapt to Vim, but sorry just can’t.

Previous solution

Before I used SFTP package to edit remote files. It works OK, but need extra effort to find the desired files from pop window like the one below.

Imgur

I also wrote a post before what package I used in Atom, FTP and markdown packages in Atom. The only problem I have with Atom is: it is VERY slow. “Slow” I mean from double click to actually typing. For that reason, I mostly used [Notepad++]. It’s fine if you like to open Atom window all the time, but I personaly like to close un-used windows/tabs. Sublime Text has a comparable speed with Notepad++, though a little lag. But with much more features, I can take it.

New solution Using rmate

I did the following to connect Sublime Text with Server.

1. Install rmate on server

Rmate can set up a tunnel between server and local computer. I used Rubygems to install, following official instruction. The difference is I have to install for user by user-install. You can find more detail from Rubygem FAQ.

# add --user-install
gem install rmate --user-install

By default, it was installed in ~/.gem/ruby/1.9.1/bin. Then need to add that directory into PATH.

# add the following code in ~/.bashrc. Restart session.
if which ruby >/dev/null && which gem >/dev/null; then
    PATH="$(ruby -rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi

Now rmate is a excutable command.

2. Install rsub package on Sublime Text

To install rsub on Sublime Text, the easier way is to install through Package Control. I skip this part, shoud be very easy. Just Ctrl + Shift + P, then Install Package and rsub.

You can see the package default setting from Package Settings. It uses port 52698 by default. See code below.

/*
    rsub default settings
*/
{
    /*
        rsub listen port
    */
    "port": 52698,

    /*
        rsub listen host

        WARNING: it's NOT recommended to change this option,
        use SSH tunneling instead.
    */
    "host": "localhost"
}

3. Connect rmate with Sublime Text in Putty.

So far we set up two parts: server and local, however we did not connect them. If you type rmate test.txt, you probably will get the following error Error connecting to ‘localhost:52698’: Connection refused - connect(2).

To connech SHH tunnels, first load saved session for your server.

Next, Click SSH –> Tunnels. Put 52698 in Source port and localhost:52698 in Destination. Don’t forget to choose remote.

Imgur

Then, click Add, you will see the information in the blank above. Now go back to your session and Save, then you don’t need to config this everytime.

Imgur

Here you go! You are all set!!

4. Test a file.

Let’s try it. Create a test.txt file.

jhuang@pauper:~$ cat test.txt
can you see me in Sublim text?

To edit it, make sure the Sublime Text is opened in your local machine. Then type rmate test.txt. You should be able to see the file open in your editor, took less than a second for me.

Added one a sentence, Ctrl + S and close the file.

jhuang@pauper:~$ cat test.txt
can you see me in Sublim text?

Yes I can see it! Awesome!!

Whoo!! Done!

I’m very happy about it. Hope you find it’s helpful too. This post on setting instructions is also very helpful. Thanks to the author.


Edited 2018-06-27

I started to use MobaXterm since last week. It offers a lot of nice features, for example, built-in X11 forwarding. Overall, it’s really good terminal. However, rsub/rmate setting in MobaXterm is different from Putty. Here are the settings I use and it works.

Step1: Open MobaXterm. On the top, click an icon called Tunneling.

Imgur

Step2: From the popup window, click New SSH tunnel.

Imgur

Step3: Fill all blanks in the next window. Most of settings are the same as Putty. Make sure to put the server IP and your login name.

Imgur

Step4: Now your tunnel has been set. But it does need to be turn on.

Imgur

It may take ~1min to be able to use rmate. Works on my desktop. Hope this works for you too!