#!/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.
#
# Usage:
# 1. Open an integrated terminal in vscode, and run codesrv-connect
# 2. In the target shell, cd to the same directory and run
# ". .codesrv-connect", or follow the instruction printed by codesrv-connect.
#
# Setup:
# Put "codesrv-connect somewhere on your PATH (e.g. ~/bin)"
#
# Cleanup:
# - Delete abandoned .codesrv-connect files when their vscode sessions die.
# - Do not add .codesrv-connect files to git repositories.
#
# 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.0.0 - les.matheson@gmail.com - 2020-03-31
#
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]"
if [[ $(which code) != *vscode-server* ]]; then
errExit "The 'code' command doesn't refer to something under .vscode-server: $(type -a code)"
fi
cat <<EOF >.codesrv-connect
# 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"
alias code=$(which code)
echo "Done: the 'code' command will talk to socket \"$VSCODE_IPC_HOOK_CLI\" now."
echo "You can delete .codesrv-connect when the vscode server context dies, or reuse it in other shells until then."
fi
EOF
echo "# OK: run this to connect to vscode server in a destination shell:"
echo ". $PWD/.codesrv-connect"
Visual Studio Code可以检测并在配置Terminal: Select Default Profile的菜单中列出已安装的Git Bash,正如许多其他答案已经描述的那样,但这从未发生在我身上。对于那些不像我这么幸运的人,你可以手动将自定义配置文件添加到Visual Studio Code的settings.json中:
{
// Tested in Visual Studio Code version 1.58.2, 1.59.1
// Notice: my git install path is `D:\Git\bin\bash.exe`
//"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe",
// This works fine for me for a long time,
// but in latest versions this is reported as deprecated,
// you can keep this and sometimes Visual Studio Code will prompt to help
// `migrate` it into new setting.
// This part can be generated by Visual Studio Code
"terminal.integrated.profiles.windows": {
// This seems to be a reserved profile name, and also does not work for
// me
"Git Bash": {
"path": "D:\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
},
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
// Add your custom one with a different profile name from "Git Bash"
"gitbash": {
"path": "D:\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
}
},
// Set the custom profile as default
"terminal.integrated.defaultProfile.windows": "gitbash",
// ...
}