在 Windows 上,我必须在start-ssh-agent.cmd
打开的每个新终端会话上运行该命令。我的开发环境是 VSCode,我每天都会打开十几个新终端。每个终端打开后,我必须手动运行此命令。
每次我打开一个命令时,有没有办法在终端上运行这个命令?
这可能采用 VSCode 扩展、VSCode 配置(设置)或 Windows 环境配置的形式。
任何想法?
在 Windows 上,我必须在start-ssh-agent.cmd
打开的每个新终端会话上运行该命令。我的开发环境是 VSCode,我每天都会打开十几个新终端。每个终端打开后,我必须手动运行此命令。
每次我打开一个命令时,有没有办法在终端上运行这个命令?
这可能采用 VSCode 扩展、VSCode 配置(设置)或 Windows 环境配置的形式。
任何想法?
在 Linux 系统上,您应该使用:
"terminal.integrated.shellArgs.linux"
在 Windows 和 OSX 上:
terminal.integrated.shellArgs.windows
和
terminal.integrated.shellArgs.osx
分别。
如果您想shellArgs
在每个工作空间的基础上应用设置 - 您可以,尽管文档说:
第一次打开定义了这些设置的工作区时,VS Code 会警告你,然后总是忽略之后的值
至少 1.42 版的 VSCode 会询问您以下问题:
“这个工作区要设置
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
但是这样你最终会进入一个subshell。有时这是不可接受的(Read 1) (Read 2)。"terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
会将您留在初始 shell 中,但不会执行.bashrc
init 文件。所以你可能想在source ~/.bashrc
里面your_init_script.sh
#!/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"]
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
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
您可以执行以下操作:
"terminal.integrated.shellArgs.windows": ["start-ssh-agent.cmd"]
修改自:https ://code.visualstudio.com/docs/editor/integrated-terminal#_shell-arguments
我实际上为此找到了一个非常简洁的 Linux 解决方案。如果你使用像 Bash 这样的 shell,它也应该在 Windows 上工作。我不确定是否可以使用香草 CMD。
将这样的内容添加到您的.bashrc
or .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 终端窗口中自动获取。
另一个答案很好,但有点过时了。您将在 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 上将以下内容用于 powershell:
{
"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
经过多次试验和错误,以下在 OSX 上对我有用:
"terminal.integrated.shellArgs.osx": [
"-l",
"-c",
"source script.sh; bash"
],
对于上下文,我将它与 jupyter notebook 一起使用来设置不能简单地使用定义的环境变量terminal.integrated.env.osx
如果您使用 PowerShell,则可以将 PowerShell 脚本添加到您的配置文件中,您可以在其中执行所需的操作。每个开发环境有 4 个 Profile,存储在 $Profile 中:
例如,在 VSCode 中创建一个配置文件
code $profile.CurrentUserAllHosts