Skip to content

Instantly share code, notes, and snippets.

@bsharper
Created January 13, 2024 18:13
Show Gist options
  • Save bsharper/deaae59107e9c09e932eb560da2ce89b to your computer and use it in GitHub Desktop.
Save bsharper/deaae59107e9c09e932eb560da2ce89b to your computer and use it in GitHub Desktop.
Open Visual Studio Code or VSCodium from a remote host
#!/bin/bash
# This is like "code" or "codium" but on a remote device. Often when I'm writing something on a Raspberry Pi
# it can be useful to open the current folder in VSC or VSCodium on a different machine
PROJPATH="$(pwd)"
# for VSCode on macOS change to RCOMMAND="/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"
RCOMMAND="codium"
# change to device where VSC or VSCodium should be opened, this should be setup with passwordless (key-based) logins
RHOST="my_remote_host"
# allow other paths to be passed in as an argument
if [[ ${#1} -gt 0 ]]; then
PROJPATH=$(realpath "$1")
fi
if [[ ! -d "$PROJPATH" ]]; then
echo "Error: can not find project path \"$PROJPATH\""
exit 1
fi
#escape paths / commands that have spaces
PROJPATH=$(printf %q "$PROJPATH")
RCOMMAND=$(printf %q "$RCOMMAND")
# this could be hardcoded if necessary, this works for me
HNAME=$(hostname)
# print the command this is running
echo "ssh $RHOST \"$RCOMMAND --folder-uri vscode-remote://ssh-remote+$HNAME$PROJPATH\""
# set JUSTPRINT to exit here and not actually run the remote command
if [[ -z ${JUSTPRINT+x} ]]; then
ssh $RHOST "$RCOMMAND --folder-uri vscode-remote://ssh-remote+$HNAME$PROJPATH"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment