27

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

每次我打开一个命令时,有没有办法在终端上运行这个命令?

在此处输入图像描述

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

任何想法?

4

8 回答 8

23

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

"terminal.integrated.shellArgs.linux"

在 Windows 和 OSX 上:

terminal.integrated.shellArgs.windows

terminal.integrated.shellArgs.osx

分别。

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

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

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

“这个工作区要设置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
    
    但是这样你最终会进入一个subshel​​l。有时这是不可接受的(Read 1) (Read 2)
  2. "terminal.integrated.shellArgs.linux": ["--init-file", "your_init_script.sh"]
    
    会将您留在初始 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
    
  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
于 2020-02-27T17:06:07.150 回答
18

您可以执行以下操作:

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

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

于 2017-08-11T13:00:26.317 回答
16

我实际上为此找到了一个非常简洁的 Linux 解决方案。如果你使用像 Bash 这样的 shell,它也应该在 Windows 上工作。我不确定是否可以使用香草 CMD。

将这样的内容添加到您的.bashrcor .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 终端窗口中自动获取。

于 2020-02-27T17:12:29.090 回答
3

另一个答案很好,但有点过时了。您将在 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
于 2021-11-19T02:22:03.463 回答
2

我在 Windows 上将以下内容用于 powershell:

{
    "terminal.integrated.shellArgs.windows": [
        "-NoExit",
        "-Command", "conda activate ./env"
    ]
}
于 2020-04-09T11:44:44.693 回答
2

对于使用美妙的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

于 2019-12-16T23:05:12.323 回答
1

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

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

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

于 2020-11-12T04:27:13.950 回答
0

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

  • 所有用户所有主机
  • AllUsersCurrentHost
  • 当前用户所有主机
  • 当前用户当前主机

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

code $profile.CurrentUserAllHosts

更多细节

于 2020-02-25T15:42:25.760 回答