Windows上的Visual Studio Code默认使用PowerShell作为集成终端。如果你想从Visual Studio Code中使用Bash,应该遵循哪些步骤?
当前回答
分离的或不相关的shell和代码[参数]支持呢?
虽然其他答案讨论了如何配置和使用VScode集成的WSL bash终端支持,但他们并没有解决“分离外壳”的问题:外壳不是从VScode内部启动的,或者以某种方式从与IDE相关的VScode服务器实例中“断开连接”。
这样的shell会给出如下错误:
命令只能在WSL或Visual Studio Code终端中使用。
还是……
无法连接到VS Code服务器。 请求出错
下面是一个脚本,它可以很容易地解决这个问题。
我每天都使用这个工具将tmux会话中的shell与特定的VScode服务器实例连接起来,或者修复从其宿主IDE分离的集成shell。
#!/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的最新更新,事情发生了一些变化。下面的步骤对我有用。
按Ctrl + Shift + P打开Visual Studio Code命令菜单。 在文本区输入>preferences: Open Settings (JSON)。 在JSON文件的末尾添加以下行 右边窗格。 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" 关闭并重新打开Visual Studio Code实例。
这个答案与投票最多的答案相似,但有一个重要的区别:之前关于这个问题的很多答案都集中在运行Git Bash上,而我的答案则集中在运行WSL Bash上。
在Windows 10机器上启用Windows子系统for Linux。 打开Visual Studio Code,按住Ctrl + '打开终端。 使用Ctrl + Shift + P打开命令面板。 类型—选择“Default Shell”。 从选项中选择WSL Bash(不是Git Bash)。
单击终端窗口中的+图标。新终端现在将是一个WSL Bash终端!
Ctrl+Shift+P >打开用户设置>
"terminal.integrated.profiles.windows": {
"Git Bash": {
"path": "D:\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
}
},
"terminal.integrated.defaultProfile.windows": "Git Bash",
"git.enabled": true,
"git.path": "D:\\Git\\bin\\bash.exe",
"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe"
如果您在工作区!
Ctrl+Shift+P >打开工作区设置>在“设置”中粘贴以下内容:{}:
"terminal.integrated.profiles.windows": {
"Git Bash": {
"path": "D:\\Git\\bin\\bash.exe",
"icon": "terminal-bash"
}
},
"terminal.integrated.defaultProfile.windows": "Git Bash",
"git.enabled": true,
"git.path": "D:\\Git\\bin\\bash.exe",
"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe"
最后关闭你所有的Vscode窗口/实例,重新打开,你就可以开始了。
关于莱昂布洛伊和灵魂闪耀 解决方案!
Visual Studio Code可以检测并在配置终端菜单中列出已安装的Git Bash:选择默认配置文件,正如许多其他答案已经描述的那样,但这从未发生在我身上。对于那些不像我这么幸运的人,你可以在Visual Studio Code的设置中添加自定义配置文件。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",
// ...
}
在最新的稳定版本中,即1.54,禁用ConPTY解决了我的问题。我写这篇文章是希望它也能解决你的问题。
{
"terminal.integrated.shell.windows": "C:\\path\\to\\bin\\bash.exe",
"terminal.integrated.windowsEnableConpty": false
}
推荐文章
- Visual studio代码-键盘快捷键-全部展开/折叠
- Visual Studio Code无法检测已安装的Git
- 禁用Visual Studio代码中的工具提示提示
- Visual Studio Code -将空格转换为制表符
- 在VSCode中的列自动硬换行
- Visual Studio Code:格式没有使用缩进设置
- 如何排除文件扩展名和语言从“格式保存”在VSCode?
- Visual Studio代码- URI的目标不存在" package:flutter/material.dart "
- VSCode——如何设置调试Python程序的工作目录
- 是否可以配置控制+滚轮增加/减少缩放在VS Code?
- 如何禁用或隐藏滚动条/小地图?
- Visual Studio代码如何解决合并冲突与git?
- Visual Studio代码-在文件末尾插入换行符
- 如何重新启动VScode编辑扩展的配置?
- Visual Studio代码PHP Intelephense保持显示不必要的错误