【问题标题】:VSCode: How to run a command after each terminal open?VSCode:每个终端打开后如何运行命令?
【发布时间】:2023-03-17 05:55:01
【问题描述】:

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

有没有一种方法可以在我每次打开终端时在终端上运行这个命令?

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

有什么想法吗?

【问题讨论】:

    标签: visual-studio-code vscode-extensions vscode-settings vscode-tasks


    【解决方案1】:

    在 Linux 系统上您应该使用:

    "terminal.integrated.shellArgs.linux"
    

    在 Windows 和 OSX 上:

    terminal.integrated.shellArgs.windows
    

    terminal.integrated.shellArgs.osx
    

    分别。

    如果您想针对每个工作区应用 shellArgs 设置 - 您可以,尽管 documentation 说:

    当你第一次打开定义了这些设置的工作区时,VS Code 会警告你并且随后总是忽略这些值

    至少 1.42 版的 VSCode 会询问您以下问题:

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

    See issue 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
      
      但是这样你最终会进入一个subshel​​l。有时这是不可接受的(Read 1)(Read 2)
    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
      
    3. 如果您已经在存储库中拥有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
      

      在 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不会只是执行脚本并退出吗?
    • 经过一番挖掘找到了更好的解决方案:--init-file。更新了答案。这也可能会有所帮助serverfault.com/questions/368054/…
    • 好吧,手册页上写着Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive
    • @Hubro,你是对的。这花了我一段时间。但我想我做到了。更新了答案。
    • 请注意,要在设置 ui(而不是 json)中添加 ["--init-file", "your_init_script.sh"],您需要将 --init-fileyour_init_script.sh 作为两个单独的项目添加。
    【解决方案2】:

    您可以执行以下操作:

    "terminal.integrated.shellArgs.windows": ["start-ssh-agent.cmd"]
    

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

    【讨论】:

    • 使用 /K 命令将命令处理为终端中的即时命令,例如 set CLASSPATH, ["/K", "C:\\cmder\\vendor\\init.bat"]
    【解决方案3】:

    我实际上为此找到了一个非常简洁的 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 终端窗口中自动获取。

    【讨论】:

    • 真的很有趣!感谢分享@Hubro
    • 它看起来比我的答案更好。但它不需要编辑每个团队成员的.bashrc 吗?因此,如果ZSH_INIT_COMMAND(或与此相关的任何名称)已在某人的.bashrc 中使用,则需要额外的工作、额外的文档,甚至可能引入冲突。
    • @x00 是的,但没关系。与团队共享的唯一部分是开发会话设置脚本。其他一切都在我的 PC 本地。我的团队成员可以选择手动获取脚本,也可以根据需要复制我的设置。
    • 这太优雅了!谢谢大佬!
    【解决方案4】:

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

        "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
    

    【讨论】:

      【解决方案5】:

      我在 Windows 上为 powershell 使用以下内容:

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

      【讨论】:

        【解决方案6】:

        对于使用精彩 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"
            ],
        }
        

        然后您可以将任何别名添加到应该已经存在于%CMDER_ROOT%\\config\\user_aliases.cmd 中的user_aliases.cmd 文件中

        【讨论】:

          【解决方案7】:

          经过多次试验和错误,以下在 OSX 上对我有用:

          "terminal.integrated.shellArgs.osx": [
              "-l",
              "-c",
              "source script.sh; bash"
          ],
          

          对于上下文,我将它与 jupyter notebook 一起使用来设置不能简单地使用 terminal.integrated.env.osx 定义的环境变量

          【讨论】:

            【解决方案8】:

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

            • AllUsersAllHosts
            • AllUsersCurrentHost
            • CurrentUserAllHosts
            • CurrentUserCurrentHost

            例如,使用

            在 VSCode 中创建配置文件
            code $profile.CurrentUserAllHosts
            

            Some more details

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-15
              • 2021-12-09
              • 2016-02-14
              相关资源
              最近更新 更多