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

48

在 Windows 系统中,每次打开一个新终端(terminal)时,我都需要运行命令 start-ssh-agent.cmd。我的开发环境是 VSCode,我每天会打开十几个终端,每次都需要手动运行这个命令。

有没有办法在每次打开终端时自动运行这个命令呢?

图片描述

可能可以通过 VSCode 扩展、VSCode 配置或 Windows 环境配置等方式来实现。

有什么想法吗?

10个回答

32

在 Linux 系统中你应该使用:

"terminal.integrated.shellArgs.linux"

在Windows和OSX系统上:

terminal.integrated.shellArgs.windows

terminal.integrated.shellArgs.osx
分别适用于每个工作区的话,您可以应用“shellArgs”设置,尽管文档中说:

当您打开定义这些设置的任何工作区时,VS Code会警告您,并在此后始终忽略这些值

至少从VSCode 1.42版本开始,会询问您是否想要:

"允许此工作区设置shellArgs吗?"

详见问题19758


在Linux上,默认使用bash(VSCode中的默认Shell)时,有一些微妙之处:

  1. "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
    
  2. 如果您已经在存储库中拥有some_init_script.sh,但因某种原因不想将source ~/.bashrc添加到其中,则可以使用以下方法:
  3. "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

这个答案肯定不止这些内容吧?如果你只是将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 - x00
好的,man手册上说:“如果shell是交互式的,则执行文件中的命令,而不是标准的个人初始化文件~/.bashrc。” - Hubro
这是一个非常详尽的答案。向你致敬! - touch my body
3
terminal.integrated.shellArgs现在已经弃用 - 请参考下面@Robert的答案获取现代解决方案。 - Jordan Mitchell Barrett
显示剩余2条评论

26

实际上,我找到了一个非常好的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终端窗口中被调用。


1
真的很有趣!感谢分享 @Hubro - Sébastien
它看起来比我的答案更好看。但是难道不需要编辑每个团队成员的.bashrc文件吗?因此需要额外的工作、额外的文档,并且如果ZSH_INIT_COMMAND(或任何名称)已经在某人的.bashrc中使用,甚至可能会引入冲突。 - x00
@x00 是的,但没关系。这个只有开发会话设置脚本是与团队共享的。其他所有内容都是本地在我的电脑上。我的团队成员可以选择手动获取脚本,或者如果他们想要的话可以复制我的设置。 - Hubro
1
这太优雅了!谢谢,伙计! - FlySoFast
这是唯一一个适用于zsh(没有--init-file等效项)且不启动子shell的答案。非常好的解决方案,除了x00在早期评论中提到的缺点。 - jeff-h
@jeff-h,这对我来说似乎有效:"args": ["-c", "source ${workspaceFolder}/.vscode/terminal/zsh-init.sh; zsh"] - Maxim Mazurok

20

3
使用 /K 命令在终端中作为即时命令处理命令,例如设置 CLASSPATH。例如:["/K", "C:\\cmder\\vendor\\init.bat"] - Voyager
此解决方案已被弃用。当前的VSCode版本中,实际解决方案是使用自动化配置文件,正如Robert在https://stackoverflow.com/a/70029070/3408335中提到的。 - undefined

19

其他答案很好,但有点过时。在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

4

我在Windows上使用以下命令:

{
    "terminal.integrated.shellArgs.windows": [
        "-NoExit",
        "-Command", "conda activate ./env"
    ]
}

3

如果您使用了很棒的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中。


2
在2021年4月更新后,配置结构已经发生了变化。请查看文档
即使在终端实例之间也有区别。要在Windows上运行文件:

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"
}

1

我为了访问 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",
}


1

如果您使用PowerShell,您可以将PowerShell脚本添加到您的个人资料中,以执行您想要的操作。每个开发环境都有4个配置文件,存储在$Profile中:

  • AllUsersAllHosts
  • AllUsersCurrentHost
  • CurrentUserAllHosts
  • CurrentUserCurrentHost

例如,在VSCode中创建一个配置文件:

code $profile.CurrentUserAllHosts

更多细节


1
在经过多次尝试后,以下方法适用于我在OSX上的情况:
"terminal.integrated.shellArgs.osx": [
    "-l",
    "-c",
    "source script.sh; bash"
],

为了提供背景信息,我将在jupyter笔记本中使用它来设置无法简单定义的环境变量terminal.integrated.env.osx


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接