My solution for editing remote files locally

When it comes to edit configuration files on a remote server, the fastest way is usually to ssh into that server and use “vi” or other terminal based text file editing program.

Even though I use “vi” quite often and know it enough for what I do, I appreciate the convenience and comfort of a nice text editor on my workstation. To bridge the gap I came with a solution that seems to work very well for me and I figured it might help you too.

I use a Mac as a workstation and my editor of choice has been Visual Studio Code from Microsoft, but I think the principles are generic and you can probably tweak the following to work in your own environment.

Remote VS Code

The first piece is a plugin for VS Code called “Remote VSCode“, you will find it under the plugin section of VS Code and is quickly installed. I just changed my configuration file to run remote editing at launch, it is completely transparent and doesn’t change the way you use VS Code.

This line goes in the JSON configuration file that you can edit from the Preferences menu under “Settings”. Be careful with the syntax, and add this line in the right context with the right commas to keep a valid JSON syntax.

"remote.onstartup": true

Just to show you in context, this is my whole VS Code config file, very simple :

// Place your settings in this file to overwrite the default settings
{
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/*.pyc":true
    },
    "editor.minimap.enabled": false,
    "remote.onstartup": true
}

SSH Tunneling

You need to establish some kind of communication between the remote host and the local computer your are using. This is pretty easy with SSH Tunnels, but nobody wants to type the tunnel syntax to establish the right tunnel with the right port.

I added an entry in my user’s ssh configuration file to always create a tunnel for port 52698 (the default port used by Remote VS Code. Here is the entry in my ~/.ssh/config file :

host *
    RemoteForward 52698 127.0.0.1:52698

It works quite well if you’re the only one working this way, but you might want to use a different port if multiple people are doing the same thing on the same server, otherwise the second person connecting will be denied tunnelling port 52698.

rmate

The last part is a piece of program you need to call on the server instead of “vi”, “pico” or whatever you use. That’s the only thing that needs to be placed on your remote servers. Don’t mind the fact it has been made for TextMate editor, it actually works with Remote VSCode.

rmate for bash can be found here : https://github.com/aurora/rmate

just copy the “rmate” script somewhere in your PATH and make it executable or just do this :

sudo wget -O /usr/local/bin/rmate https://raw.github.com/aurora/rmate/master/rmate
sudo chmod a+x /usr/local/bin/rmate

You can now edit a file using “rmate -p 52698 somefile.txt”, but we can avoid typing the port name every time we edit a file. For this, just create a configuration file in your home directory on the remote server :

echo "port 52698" > ~/.rmate

And voilà ! You can now connect on your server and type “rmate somefile.txt” it will open in VS Code if already running. Saving the file will send it to the remote server.

Leave a Reply

Your email address will not be published. Required fields are marked *