#!/bin/bash # codesrv-connect # # Purpose: # Copies the vscode connection environment from one shell to another, so that you can use the # vscode integrated terminal's "code [args]" command to communicate with that instance of vscode # from an unrelated shell. # # Setup: # 1. codesrv-connect should be on the PATH, e.g. in ~/bin # 2. Add "source ~/bin/codesrv-connect.rc " to your shell initialization (e.g. ~/.bashrc) # # Usage: # 1. Open an integrated terminal in vscode, and run codesrv-connect # 2. In an unrelated terminal window (i.e. wsl.exe or tmux, etc.), cd to the vscode # workspace dir and run 'codesrv' # 3. Now the unrelated terminal has a 'code' command which communicates with the vscode # instance started in step #1 # # Cleanup: # - Delete abandoned .codesrv-connect files by running codesrv-status # - Add .codesrv-connect to .gitignore # # Notes: # The VSCODE_IPC_HOOK_CLI environment variable points to a socket which is rather volatile, while the long path for the 'code' alias is more stable: vscode doesn't change the latter even across a "code -r ." reload. But the former is easily detached and so you need a fresh value if that happens. This is what codesrv-connect does: it captures the value of these two and writes them to .codesrv-connect in the current dir. # # Verinfo: v1.1.0 - les.matheson@gmail.com - 2020-04-27 # function errExit { echo "ERROR: $@" >&2 exit 1 } [[ -S $VSCODE_IPC_HOOK_CLI ]] || errExit "VSCODE_IPC_HOOK_CLI not defined or not a pipe [$VSCODE_IPC_HOOK_CLI]. Start an integrated terminal in vscode and then run codesrv-connect within that shell." if [[ $(which code) != *vscode-server* ]]; then errExit "The 'code' command doesn't refer to something under .vscode-server: $(type -a code). Start an integrated terminal in vscode and then run codesrv-connect within that shell." fi if ! which pathadd &>/dev/null; then errExit "Cant find pathadd on the PATH" fi codepath=$(echo $PATH | awk '/vscode-server/ {print $1}' RS=":") cat >.codesrv-connect <<- EOF # Temp file created by $(which codesrv-connect): source this into your working # shell like '. .codesrv-connect' # ( git hint: add ".codesrv-connect" to .gitignore ) # cd "$PWD" if ! test -S "$VSCODE_IPC_HOOK_CLI"; then echo "ERROR: $VSCODE_IPC_HOOK_CLI not a socket. Dead session." else export VSCODE_IPC_HOOK_CLI="$VSCODE_IPC_HOOK_CLI" if ! PATH="${codepath}:\$PATH" codesrv-status; then echo "ERROR: dead vscode connection." read -n 1 -p "Delete dead context info $VSCODE_IPC_HOOK_CLI ? (y/n): " if [[ \$REPLY =~ [yY] ]]; then rm $VSCODE_IPC_HOOK_CLI rm .codesrv-connect fi else alias code=$(which code) PATH=\$(pathadd "${codepath}") echo "OK: the 'code' command will talk to socket \"$VSCODE_IPC_HOOK_CLI\" now." echo "You can use this command to connect other shells to this code server:" echo " cd $PWD; codesrv" fi fi EOF if [[ $? == 0 ]] && [[ $(realpath $PWD) != $(realpath $HOME) ]]; then ln -sf $(realpath $PWD)/.codesrv-connect $HOME/ fi echo "# OK: run 'codesrv' to connect to vscode server in a destination shell, or this command:" echo ". $PWD/.codesrv-connect"