Editing files on a remote server via SSH can be tedious and annoying. It does not matter whether you work with vim, nano, or pico. Personally, I am not a fan of these editors, because I just don't feel comfortable with them. But what I feel comfortable with is Sublime. That's why I'll show you today how to open and edit remote files comfortably with Sublime directly from the terminal.
We use a plugin called
RemoteSubl locally for Sublime, which brings
rmate to Sublime. This plugin can be easily installed via PackageControl.
rmate is installed on the server and connects back to your computer so that it can communicate directly with Sublime. The easiest way to do this is to use a reverse SSH tunnel. I will show you how this works below.
rmate must be installed on the remote server. This is also super easy. Download the binary.
curl -o /usr/local/bin/rmate https://raw.githubusercontent.com/aurora/rmate/master/rmate
Make it executable.
sudo chmod +x /usr/local/bin/rmate
Alternatively, you can rename it. I usually rename it to
subl - just like I have it locally.
mv /usr/local/bin/rmate /usr/local/bin/subl
The next and last thing we need to do is to set up the port forwarding. By default
rmate listens on port
52698. We will leave it that way for now. Normally you have to create the appropriate port forwarding for each SSH connection. And this is how you do it:
ssh -R 52698:localhost:52698 email@example.com
But since that is super annoying, we can automate the whole thing. To do this, open your SSH config and we will specify that for all hosts (alternatively you can specify that for individual hosts as well), this host forwarding should work. It is quite possible that you already have some other options set. Just add the appropriate part.
Host * RemoteForward 52698 localhost:52698
That's it! From now on you can simply connect to your servers via SSH and open files by executing the command
subl file.txt. After that, your local sublime will open and you will feel at home. Of course, you can also save your files. You can also open several files at the same time by writing several file names directly after each other.
Note: If several people want to work this way on the same server, it is necessary that each user uses a different port. This port must then also be specified by an environment variable.