在 Linux 系统中你应该使用:
"terminal.integrated.shellArgs.linux"
在Windows和OSX系统上:
terminal.integrated.shellArgs.windows
和
terminal.integrated.shellArgs.osx
分别适用于每个工作区的话,您可以应用“shellArgs”设置,尽管文档中说:
当您打开定义这些设置的任何工作区时,VS Code会警告您,并在此后始终忽略这些值
至少从VSCode 1.42版本开始,会询问您是否想要:
"允许此工作区设置
shellArgs
吗?"
在Linux上,默认使用bash
(VSCode中的默认Shell)时,有一些微妙之处:
"terminal.integrated.shellArgs.linux": ["your_init_script.sh"]
将执行脚本并立即关闭终端。为了防止这种情况发生,您需要以$SHELL
命令结束脚本。#!/bin/bash
echo "init"
export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
$SHELL
但是这样你最终会进入一个子shell。有时这是不可接受的。(阅读1)(阅读2)。"terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
将保留初始Shell状态,但不执行 .bashrc 初始化文件。因此,您可能需要在 your_init_script.sh 中运行 source ~/.bashrc。#!/bin/bash
source ~/.bashrc
echo "init"
export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
some_init_script.sh
,但因某种原因不想将source ~/.bashrc
添加到其中,则可以使用以下方法:"terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
#!/bin/bash
source ~/.bashrc
source some_init_script.sh
some_init_script.sh#!/bin/bash
echo "init"
export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
在VSCode之外,您可以不创建额外的文件来完成操作。就像这样bash --init-file <(echo "source ~/.bashrc; source some_init_script.sh")
但我无法将这个字符串传递到terminal.integrated.shellArgs.linux
- 它需要以某种方式拆分成数组。而我尝试过的任何组合都没有起作用。
另外,您可以在特定文件夹中打开终端:
terminal.integrated.cwd
更改环境:
"terminal.integrated.env.linux"
"terminal.integrated.env.windows"
"terminal.integrated.env.osx"
甚至可以按照您的喜好更改终端
terminal.integrated.shell.linux
terminal.integrated.shell.windows
terminal.integrated.shell.osx
或者
terminal.external.linuxExec
terminal.external.osxExec
terminal.external.windowsExec
实际上,我找到了一个非常好的Linux解决方案。如果您使用类似Bash的shell,在Windows上也应该可以运行。我不确定是否可以使用原始CMD。
将以下内容添加到您的.bashrc
或.zshrc
中:
#
# Allow parent to initialize shell
#
# This is awesome for opening terminals in VSCode.
#
if [[ -n $ZSH_INIT_COMMAND ]]; then
echo "Running: $ZSH_INIT_COMMAND"
eval "$ZSH_INIT_COMMAND"
fi
现在,在您的VSCode工作区设置中,您可以像这样设置环境变量:
"terminal.integrated.env.linux": {
"ZSH_INIT_COMMAND": "source dev-environment-setup.sh"
}
现在脚本“dev-environment-setup.sh”会自动在所有新的VSCode终端窗口中被调用。
.bashrc
文件吗?因此需要额外的工作、额外的文档,并且如果ZSH_INIT_COMMAND
(或任何名称)已经在某人的.bashrc
中使用,甚至可能会引入冲突。 - x00--init-file
等效项)且不启动子shell的答案。非常好的解决方案,除了x00在早期评论中提到的缺点。 - jeff-h"args": ["-c", "source ${workspaceFolder}/.vscode/terminal/zsh-init.sh; zsh"]
- Maxim Mazurok您可以执行以下操作:
"terminal.integrated.shellArgs.windows": ["start-ssh-agent.cmd"]
修改自: https://code.visualstudio.com/docs/editor/integrated-terminal#_shell-arguments
["/K", "C:\\cmder\\vendor\\init.bat"]
。 - Voyager其他答案很好,但有点过时。在VSCode中,你会收到警告。以下是我在我的XXX.code-workspace
文件中在Linux上所做的:
"terminal.integrated.profiles.linux": {
"BashWithStartup": {
"path": "bash",
"args": [
"--init-file",
"./terminal_startup.sh"
]
}
},
"terminal.integrated.defaultProfile.linux": "BashWithStartup"
请确保您的terminal_startup.sh
脚本是可执行的:
chmod u+x terminal_startup.sh
我在Windows上使用以下命令:
{
"terminal.integrated.shellArgs.windows": [
"-NoExit",
"-Command", "conda activate ./env"
]
}
如果您使用了很棒的cmder,则需要在您的settings.json
中添加以下内容:
{
"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.env.windows": {
"CMDER_ROOT": "C:\\path\\to\\cmder"
},
"terminal.integrated.shellArgs.windows": [
"/k",
"%CMDER_ROOT%\\vendor\\bin\\vscode_init.cmd"
],
}
然后您可以将任何别名添加到user_aliases.cmd
文件中,该文件应已存在于%CMDER_ROOT%\\config\\user_aliases.cmd
中。
PowerShell
{
"terminal.integrated.profiles.windows": {
"My PowerShell": {
"path": "pwsh.exe",
"args": ["-noexit", "-file", "${env:APPDATA}PowerShellmy-init-script.ps1"]
}
},
"terminal.integrated.defaultProfile.windows": "My PowerShell"
}
命令提示符
{
"terminal.integrated.profiles.windows": {
"cmder": {
"path": "C:\\WINDOWS\\System32\\cmd.exe",
"args": ["/K", "C:\\cmder\\vendor\\bin\\vscode_init.cmd"]
}
},
"terminal.integrated.defaultProfile.windows": "cmder"
}
我为了访问 x64 Native Tools Command Prompt for 2022
做了以下操作:
{
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"x64 Native": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": [
"/K",
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
],
"icon": "terminal-cmd"
},
},
"terminal.integrated.defaultProfile.windows": "x64 Native",
}
如果您使用PowerShell,您可以将PowerShell脚本添加到您的个人资料中,以执行您想要的操作。每个开发环境都有4个配置文件,存储在$Profile中:
例如,在VSCode中创建一个配置文件:
code $profile.CurrentUserAllHosts
"terminal.integrated.shellArgs.osx": [
"-l",
"-c",
"source script.sh; bash"
],
为了提供背景信息,我将在jupyter笔记本中使用它来设置无法简单定义的环境变量terminal.integrated.env.osx
shellArgs.linux
设置为一个脚本,那么shell只会执行这个脚本并退出,对吗? - Hubro--init-file
。已更新答案。 此外,这可能会有所帮助:https://serverfault.com/questions/368054/run-an-interactive-bash-subshell-with-initial-commands-without-returning-to-the?newreg=627eeaeb1c9741c6944c3b83fa0db39d - x00terminal.integrated.shellArgs
现在已经弃用 - 请参考下面@Robert的答案获取现代解决方案。 - Jordan Mitchell Barrett