Skip to content

Instantly share code, notes, and snippets.

@koush
Last active February 23, 2022 23:54
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save koush/51ee5e4f72e42224b9a06bbbc3cd3796 to your computer and use it in GitHub Desktop.
Save koush/51ee5e4f72e42224b9a06bbbc3cd3796 to your computer and use it in GitHub Desktop.
Put this in your .bash_profile(s) to open VS Code on your local machine while SSH'd into a remote machine.
# this needs to be on the *remote* machine.
if [ ! -z "$SSH_CLIENT" ]
then
function code() {
local ssh_client_host=$(echo $SSH_CLIENT | cut -d ' ' -f1)
if [ -z "$1" ]
then
local argpath="."
else
local argpath="$1"
fi
# ssh-remote requires a directory
local resolved=$(realpath -m $argpath)
if [ -f "$resolved" ]; then
local resolved=$(dirname $resolved)
fi
ssh $ssh_client_host code \
--folder-uri=vscode-remote://ssh-remote+$(hostname)$resolved
}
fi
@kevinnls
Copy link

kevinnls commented Jan 20, 2022

neat!

i'd prefer wrapping the code() definition itself in the if statement checking for SSH_CLIENT. that way the init file simply doesn't define the code function unless it's been started over ssh. this way there's no complication in local logins mode =D

also using local variables removes the need to unset afterwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment