Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Explosion-Scratch/1ab013d74c001453113dccb02076d039 to your computer and use it in GitHub Desktop.
Save Explosion-Scratch/1ab013d74c001453113dccb02076d039 to your computer and use it in GitHub Desktop.
Open local VSCode instance from SSH terminal
#!/usr/bin/env bash
# Put this file in PATH somewhere, e.g. ~/.local/bin/code then use like regular VSCode code cli
# Examples:
# code --wait ~/.zshrc # Will open up VSCode on the local machine and wait until you've finished editing
# code . # Opens current directory
# Requirements:
# - The SSH VSCode extension
# - Two machines that can SSH into each other
# Initialize variables
file=""
opts=""
# Loop through all arguments
for arg in "$@"; do
if [[ $arg == --* ]]; then
# If the argument starts with --, it's an option
opts="$opts $arg"
elif [[ -z $file ]]; then
# If it's not an option and file is not set, assume it's the file
file="$arg"
else
# If file is already set and it's not an option, add to opts
opts="$opts $arg"
fi
done
# Trim leading space from opts
opts="${opts# }"
IP=$(echo $SSH_CLIENT | awk '{print $1}')
# Use realpath to support path resolution too! ~/.zshrc should resolve to $HOME/.zshrc
ssh -q $IP "/usr/local/bin/code $opts -r --remote ssh-remote+$USER@$HOSTNAME $(realpath ${file:-$PWD})"
# To use this as your default editor for git, and when SSH'ing do add the following to ~/.zshrc:
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR="$BIN_DIR/code"
export GIT_EDITOR_ARGS="--wait"
else
# Your preferred editor here
export EDITOR='nano'
fi
export VISUAL="$EDITOR"
git config --global core.editor "$EDITOR $GIT_EDITOR_ARGS"
# Now your default editor is either nano or vscode on the ssh connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment