Remote File Editing Over SSH with Visual Studio Code

Recently, I needed to add an entry to the local domain name server on my home network. For many years, I have used Emacs to edit files on the terminal both locally and on remote systems. However, over the past year or so, I’ve become familiar with Visual Studio Code, and I very much enjoy its editing experience. I searched through the available extensions and found one called Remote VSCode.


It was originally developed for TextMate, but the Remote VSCode extension makes it work with Visual Studio Code. I installed the extension and then followed the link on the extension description page to download the Bash version of rmate, the script which enables the remote side of the editing experience. I renamed the script and placed mine in /usr/local/bin/rcode on my remote system.

Then, I edited my SSH configuration (~/.ssh/config) to automatically forward the appropriate port required:

Host *
    ForwardAgent yes
    RemoteForward 52698 127.0.0.1:52698

Next, I connected to my remote system via SSH within the Visual Studio Code terminal window:

Finally, I edited my local nameserver configuration using the rcode script.

The file appears directly within Visual Studio Code, allowing you to make changes and save the file remotely. Easy, right?

While the extension does not allow browsing the remote filesystem to search for and edit files, it does allow you to easily navigate using your shell and edit multiple files simultaneously using Visual Studio Code’s tabs. It’s been a big help to me.

Conversation
  • steve poling says:

    Excellent post, Jordan, but I have a question/suggestion, when describing the steps you follow to setup the system, please make it clear which machine (local or remote) you’re talking about.

    E.g. when you changing ~/.ssh/config is it the local, remote, or both machines?

    thanks for the tip.

  • steve poling says:

    …and you have to tell VS Code to “start server”

  • Ladislav Prskavec says:

    SSH config you need just on local and don’t forget start server that was my issue.

  • Sidharth Yatish says:

    I have a problem. If I use rsub from the VS code bash terminal, I get connection refused error. To overcome this, I have to manually open putty and use rsub once. After that rsub command from vscode terminal works. Am I doing it wrong?

    • Sidharth Yatish says:

      I have enabled port forwarding in putty settings that works. Port forwarding in ~/.ssh/config doesnt seem to work

  • Gilles Dubreuil says:

    Alternatively one can use SSHFS [1] to bring part or whole remote filesystem to local machine. Then open the desired files with your favorite editor.

    [1] https://github.com/libfuse/sshfs

    • Anonymous says:

      I use this method. There is an extension for VS Code available for SSHFS, so you can also open and browse remote directories.

  • Dima Fomin says:

    Phew! Seems too complex for me. Please try an extention “SSH FS”
    https://marketplace.visualstudio.com/items?itemName=Kelvin.vscode-sshfs

    There just config of 7 lines and then you’ll get just normal remote Workspace of remote host

  • Arto says:

    To be honest, I prefer winscp sync from Windows over the specific file I would like to edit So any change done by VS Code is automatically synced on the target server.

  • Comments are closed.