创建多个终端并在VSCode中运行命令

时间:2020-03-10 15:56:03

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

我在Mac上?。我试图探索一种方法,只要在我的工作区文件中单击一下即可创建4个终端。 我尝试过工作,但似乎卡住了

{
    "folders": [
        {
            "path": "/Users/bheng/Sites/laravel/project"
        }
    ],
    "settings": {
        "workbench.action.terminal.focus": true,
        "terminal.integrated.shell.osx": "ls",
        "terminal.integrated.shellArgs.osx": [
            "ls -lrt"
         ]
    },
    "extensions": {}
}

我的目标是开设4个航站楼

  • Terminal1:运行“ npm run watch”
  • Terminal2:运行'ls -lrt'
  • Terminal3:运行“ ssh_staging”
  • Terminal4:运行“ mysql”

我一直在关注此文档:https://code.visualstudio.com/docs/editor/integrated-terminal#_terminal-keybindings

有什么提示吗?

1 个答案:

答案 0 :(得分:5)

我一直在玩这个似乎可行的方法。结合在打开的文件夹上运行任务并使该任务依赖于其他任务的能力,我想到了以下内容。它看起来很麻烦,但实际上非常简单且重复。

首先,您将需要一个像multi-command这样的宏扩展名。将其放入您的设置:

"multiCommand.commands": [

    {
      "command": "multiCommand.runInFirstTerminal",
      "sequence": [
        "workbench.action.terminal.new",
        {
          "command": "workbench.action.terminal.renameWithArg",
          "args": {
            "name": "npm watch"
          }
        },
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "npm run watch\u000D"  // \u000D is a return so it runs
          }
        }
      ]
    },
    {
      "command": "multiCommand.runInSecondTerminal",
      "sequence": [
        "workbench.action.terminal.new",
        {
          "command": "workbench.action.terminal.renameWithArg",
          "args": {
            "name": "ls -lrt"
          }
        },
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "ls -lrt\u000D"
          }
        }
      ]
    },
    {
      "command": "multiCommand.runInThirdTerminal",
      "sequence": [
        "workbench.action.terminal.new",
        {
          "command": "workbench.action.terminal.renameWithArg",
          "args": {
            "name": "ssh_staging"
          }
        },
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "ssh_staging\u000D"  // however you run the ssh_staging command
          }
        }
      ]
    },
    {
      "command": "multiCommand.runInFourthTerminal",
      "sequence": [
        "workbench.action.terminal.new",
        {
          "command": "workbench.action.terminal.renameWithArg",
          "args": {
            "name": "mysql"
          }
        },
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            "text": "mysql\u000D"  // however you run the mysql command
          }
        },
        // "workbench.action.focusActiveEditorGroup"
      ]
    }
]

每个终端有一个命令。但是在每个宏中,您可以做的事情与进入宏一样多-尤其是多亏了sendSequence命令。您可以更改目录并向同一终端实例发送另一个sendSequence命令,也可以运行所有非终端命令,在最后一次终端设置结束时将焦点更改为编辑器,等等。

我添加了使用命令workbench.action.terminal.renameWithArg根据您的命令命名每个终端的好处。

在tasks.json中:

 "tasks": [

    {
      "label": "Run 4 terminals on startup",
      "runOptions": {"runOn": "folderOpen"},

      "dependsOrder": "sequence",  // or parallel

      "dependsOn": [
        "terminal1",
        "terminal2",
        "terminal3",
        "terminal4"
      ]
    },  

    {
      "label": "terminal1",
      "command": "${command:multiCommand.runInFirstTerminal}"
    },
    {
      "label": "terminal2", 
      "command": "${command:multiCommand.runInSecondTerminal}",
    },
    {
      "label": "terminal3",
      "command": "${command:multiCommand.runInThirdTerminal}"
    },
    {
      "label": "terminal4",
      "command": "${command:multiCommand.runInFourthTerminal}"
    }
 ]

现在,无论何时打开(或重新加载)工作空间文件夹,都应打开,命名和运行四个任务中的tasks.json。以我的经验,vscode运行任何folderOpen任务之前会有一个短暂的延迟。


如果您希望手动触发Run 4 terminals任务,则可以这样设置键绑定:

{
  "key": "alt+r",     // whatever keybinding you want
  "command": "workbench.action.tasks.runTask",
  "args": "Run 4 terminals on startup"
},

这里是一个使用键绑定运行的演示,比重新加载vscode更容易演示,但是没有区别。我为仅用于演示目的的每个终端添加了一个间隔延迟-否则速度非常快。

opening four terminals and running commands in them

我注意到,如果我不与其中一个终端进行交互或在删除所有终端之前先打开另一个终端,则vscode会冻结。


还有一个有趣的扩展名Terminal Manager。我还没有尝试过。

用于一次或多次设置多个终端的扩展 运行一些命令。

但是对于我来说是否可以将此扩展配置为可以在folderOpen上运行并不明显-但是它似乎贡献了run all the terminals命令,因此您应该可以在任务中使用它。