在VSCode中创建多个端子并运行命令 [英] Create multiple terminals and run commands in VSCode

查看:18
本文介绍了在VSCode中创建多个端子并运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是Mac💻。我正在尝试一种方法,只要我在我的工作区文件上按下DBL并单击,就可以创建4个Terminals。 我试过让它工作,但我似乎卡住了

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

对我有什么提示吗?

推荐答案

我一直在尝试这个方法,它似乎起作用了。结合在打开文件夹时运行任务的能力,并使该任务依赖于其他任务,我想出了以下方法。它看起来很繁琐,但实际上相当简单和重复。

首先,您需要一个类似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 watchu000D"  // 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 -lrtu000D"
          }
        }
      ]
    },
    {
      "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_stagingu000D"  // 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": "mysqlu000D"  // 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更容易演示,但没有区别。我为每个运行的终端添加了间隔延迟,仅用于演示目的-否则速度极快。

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


还有一个可能感兴趣的Terminal Manager扩展。我还没有试过。

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

但我并不清楚是否可以将此扩展配置为在folderOpen上运行,但它似乎提供了一个run all the terminals命令,因此您应该能够在任务中使用该命令。

这篇关于在VSCode中创建多个端子并运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆