VSCode:每个终端打开后如何运行命令?

Séb*_*ien 5 visual-studio-code vscode-extensions vscode-settings vscode-tasks

在Windows上,我必须在start-ssh-agent.cmd打开的每个新终端会话上运行命令。我的开发环境是VSCode,每天我都会打开十几个新终端。每个终端打开后,我必须手动运行此命令。

每次打开一个终端时,是否可以在终端上运行此命令?

在此处输入图片说明

这可以采用VSCode扩展名,VSCode配置(设置)或Windows环境配置的形式。

任何的想法?

x00*_*x00 23

在 Linux 系统上,您应该使用:

"terminal.integrated.shellArgs.linux"
Run Code Online (Sandbox Code Playgroud)

在 Windows 和 OSX 上:

terminal.integrated.shellArgs.windows
Run Code Online (Sandbox Code Playgroud)

terminal.integrated.shellArgs.osx
Run Code Online (Sandbox Code Playgroud)

分别。

如果你想shellArgs在每个工作区的基础上应用设置 - 你可以,尽管文档说:

当您第一次打开一个工作空间,它定义的任何设置,VS代码会向您发出警告,并随后一直忽略的值之后

至少 1.42 版的 VSCode 会问你类似的问题:

“这个工作区要设置shellArgs,你要允许吗?”

见 19758 期


在 Linux 上,如果您使用bash(VSCode 中的默认 shell),则有一些微妙之处:

  1. "terminal.integrated.shellArgs.linux": ["your_init_script.sh"]
    
    Run Code Online (Sandbox Code Playgroud) 将执行脚本并立即关闭终端。为了防止这种情况,您必须以结束脚本$SHELL命令。
    #!/bin/bash
    echo "init"
    export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
    $SHELL
    
    Run Code Online (Sandbox Code Playgroud) 但是那样你最终会进入一个subshel​​l。有时这是不可接受的(Read 1) (Read 2)
  2. "terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
    
    Run Code Online (Sandbox Code Playgroud) 将使您留在初始 shell,但不会执行.bashrcinit 文件。所以你可能想source ~/.bashrc进去your_init_script.sh
    #!/bin/bash
    source ~/.bashrc
    echo "init"
    export PATH=$PATH:/xxx/yyy/zzz # or do whatever you want
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果您已经some_init_script.sh在一个存储库中,并且由于某种原因不想添加source ~/.bashrc到它,您可以使用这个:
    "terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
    
    Run Code Online (Sandbox Code Playgroud) your_init_script.sh
    "terminal.integrated.shellArgs.linux"
    
    Run Code Online (Sandbox Code Playgroud) some_init_script.sh
    terminal.integrated.shellArgs.windows
    
    Run Code Online (Sandbox Code Playgroud)
    在 VSCode 之外,您无需创建额外的文件。像这样
    bash --init-file <(echo "source ~/.bashrc; source some_init_script.sh")
    
    Run Code Online (Sandbox Code Playgroud) 但是我无法将此字符串传递给terminal.integrated.shellArgs.linux- 它需要以某种方式拆分为数组。我尝试过的组合都没有奏效。

此外,您可以在特定文件夹中打开终端:

terminal.integrated.cwd
Run Code Online (Sandbox Code Playgroud)

更改环境:

"terminal.integrated.env.linux"
"terminal.integrated.env.windows"
"terminal.integrated.env.osx"
Run Code Online (Sandbox Code Playgroud)

甚至可以根据自己的喜好更改终端

terminal.integrated.shell.linux
terminal.integrated.shell.windows
terminal.integrated.shell.osx
Run Code Online (Sandbox Code Playgroud)

或者

terminal.external.linuxExec
terminal.external.osxExec
terminal.external.windowsExec
Run Code Online (Sandbox Code Playgroud)

  • `terminal.integrated.shellArgs` 现已被 **弃用** - 请参阅下面 @Robert 的答案以获取现代解决方案。 (3认同)

Rob*_*ert 22

另一个答案很好,但有点过时了。您将在 VSCode 中收到警告。这是我在 Linux 上的文件中所做的事情XXX.code-workspace

    "terminal.integrated.profiles.linux": {
      "BashWithStartup": {
        "path": "bash",
        "args": [
          "--init-file",
          "./terminal_startup.sh"
        ]
      }
    },
    "terminal.integrated.defaultProfile.linux": "BashWithStartup"
Run Code Online (Sandbox Code Playgroud)

请确保您的terminal_startup.sh脚本可执行:

chmod u+x terminal_startup.sh
Run Code Online (Sandbox Code Playgroud)


Hub*_*bro 12

在提出问题后不久,我实际上为此找到了一个非常简洁的 Linux 解决方案。如果你使用像 Bash 这样的 shell,它也应该在 Windows 上工作。我不确定是否可以使用 vanilla 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
Run Code Online (Sandbox Code Playgroud)

现在,在您的 VSCode 工作区设置中,您可以像这样设置环境变量:

"terminal.integrated.env.linux": {
    "ZSH_INIT_COMMAND": "source dev-environment-setup.sh"
}
Run Code Online (Sandbox Code Playgroud)

现在脚本“dev-environment-setup.sh”将在所有新的 VSCode 终端窗口中自动获取。


Jac*_*lda 5

您可以执行以下操作:

"terminal.integrated.shellArgs.windows": ["start-ssh-agent.cmd"]
Run Code Online (Sandbox Code Playgroud)

修改自:https : //code.visualstudio.com/docs/editor/integrated-terminal#_shell-arguments

  • 使用 /K 命令将命令作为终端中的即时命令处理,例如 set CLASSPATH, `["/K", "C:\\cmder\\vendor\\init.bat"]` (3认同)