VSCode:如何在每个终端打开后运行命令? [英] VSCode: How to run a command after each terminal open?
问题描述
在 Windows 上,我必须在打开的每个新终端会话上运行命令 start-ssh-agent.cmd
.我的开发环境是VSCode,每天新开十几个终端.每个终端打开后,我必须手动运行这个命令.
On Windows I have to run the command start-ssh-agent.cmd
on each new terminal session I open. My development environment is VSCode, and I open a dozen new terminals each day. After each terminal open, I have to manually run this command.
有没有办法在每次打开终端时在终端上运行这个命令?
Is there is a way to run this command on the terminal each time I open one ?
这可能采用 VSCode 扩展、VSCode 配置(设置)或 Windows 环境配置的形式.
This may take the form of a VSCode extension, VSCode configuration (settings) or a Windows environment configuration.
有什么想法吗?
推荐答案
在 Linux 系统上你应该使用:
On Linux systems you should use:
"terminal.integrated.shellArgs.linux"
在 Windows 和 OSX 上:
On Windows and OSX:
terminal.integrated.shellArgs.windows
和
terminal.integrated.shellArgs.osx
分别.
如果您想在每个工作区的基础上应用 shellArgs
设置 - 您可以,尽管 文档 说:
If you want to apply shellArgs
setting on a per-workspace basis - you can, despite the fact that documentation says:
第一次打开定义任何这些设置的工作区时,VS Code 会警告您,并且随后总是忽略这些值之后
The first time you open a workspace which defines any of these settings, VS Code will warn you and subsequently always ignore the values after that
至少 1.42 版的 VSCode 会问你类似的问题:
At least version 1.42 of VSCode asks you something like:
这个工作区想要设置shellArgs
,你要允许吗?"
"This workspace wants to set
shellArgs
, do you want to allow it?"
在 Linux 上,如果您使用 bash
(VSCode 中 shell 的默认设置),则有一些微妙之处:
On Linux, if you are using bash
(default for shell in VSCode), there are some subtleties:
"terminal.integrated.shellArgs.linux": ["your_init_script.sh"]
将执行脚本并立即关闭终端.为了防止这种情况,您必须使用 $SHELL
命令结束脚本.
will execute the script and close terminal right away. To prevent this you'll have to end the script with $SHELL
command.
#!/bin/bash
echo "init"
export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
$SHELL
但是那样你最终会进入一个子shell.有时这是不可接受的 (阅读 1) (阅读 2).
But that way you end up in a subshell. Sometimes it's unacceptable (Read 1) (Read 2).
"terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
将使您留在初始 shell,但不会执行 .bashrc
init 文件.所以你可能想在 your_init_script.sh
里面 source ~/.bashrc
will leave you in the initial shell, but will not execute the .bashrc
init file. So you may want to source ~/.bashrc
inside 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
添加到其中,您可以使用这:
some_init_script.sh
in a repository, and for some reason don't feel like adding source ~/.bashrc
into it, you can use this:
"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
<小时>在 VSCode 之外,您无需创建额外的文件即可完成.像这样
bash --init-file <(echo "source ~/.bashrc; source some_init_script.sh")
但我无法将此字符串传递到 terminal.integrated.shellArgs.linux
- 它需要以某种方式拆分为数组.我尝试过的所有组合都没有奏效.
But I could not pass this string into terminal.integrated.shellArgs.linux
- it needs to be split into array somehow. And none of the combinations I've tried worked.
<小时>
此外,您可以在特定文件夹中打开终端:
Also, you can open terminal at a specific folder:
terminal.integrated.cwd
更改环境:
"terminal.integrated.env.linux"
"terminal.integrated.env.windows"
"terminal.integrated.env.osx"
甚至可以根据自己的喜好更改终端
And even change terminal to your liking with
terminal.integrated.shell.linux
terminal.integrated.shell.windows
terminal.integrated.shell.osx
或
terminal.external.linuxExec
terminal.external.osxExec
terminal.external.windowsExec
这篇关于VSCode:如何在每个终端打开后运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!