Last active
June 25, 2024 16:00
-
-
Save Explosion-Scratch/1ab013d74c001453113dccb02076d039 to your computer and use it in GitHub Desktop.
Open local VSCode instance from SSH terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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