Skip to content

Instantly share code, notes, and snippets.

@noelbundick
Last active June 24, 2021 15:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelbundick/ab2dd9fc5dada84795be2ea0321444ca to your computer and use it in GitHub Desktop.
Save noelbundick/ab2dd9fc5dada84795be2ea0321444ca to your computer and use it in GitHub Desktop.
VS Code remote SSH from terminal

I've got 2 flavors of launching VS Code on remote servers for you

  1. Clean terminal, you know you want to launch code on a remote server. Check out sshcode.sh

  2. You want to launch code on your local machine from inside an existing SSH session. This needs a bit of setup, but seems to work pretty well. Hacks inbound!

  • Set PermitLocalCommand yes in your SSH config (see example)
  • Add a LocalCommand to stash the username, IP, and current directory in a local file when you connect
  • Save hack.sh in your home directory

Now to connect, use the SSH escape sequence (default single tilde ~) along with !command to invoke the script

  • Press Enter
  • Press ~
  • Type !command ~/hack.sh
  • ???
  • Congrats, you've just launched VS Code from an existing SSH session with only nominal pain. Impress your friends!
Host *
PermitLocalCommand yes
LocalCommand echo %r@%n%d > hack.ssh.txt
#!/bin/bash
# Save this in your home directory
# Remember to mark it executable: chmod +x ~/hack.sh
# Use `code` or `code-insiders` as appropriate
code-insiders --folder-uri "vscode-remote://ssh-remote+$(cat hack.ssh.txt)"
# Add this to your profile/.bashrc/etc
function sshcode() {
local REMOTE_PATH="${2:-/}"
code --folder-uri "vscode-remote://ssh-remote+$1$REMOTE_PATH"
}
# Usage
# Specify a host and a path
sshcode pi@192.168.1.10 /home/pi/home-assistant
# Browse the root if you don't specify a path
sshcode noel@52.137.118.98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment