简体   繁体   English

如何从 Visual Studio Code 的集成终端中打开一个新选项卡(不使用键盘快捷键)?

[英]How to open a new tab from inside Visual Studio Code's integrated terminal (not with keyboard shortcuts)?

I want to open a new terminal tab as part of a project I'm working on requires that I run another script in a new tab once I've got the first portion running.作为我正在处理的项目的一部分,我想打开一个新的终端选项卡,这要求我在第一部分运行后在新选项卡中运行另一个脚本。 But I am not seeing how to automate this on macOS Big Sur.但我没有看到如何在 macOS Big Sur 上自动执行此操作。 I am thinking AppleScript is the way to go, but if there's another way to run from ZSH I'd be thrilled.我在想 AppleScript 是通往 go 的路,但如果有另一种从 ZSH 运行的方式,我会很高兴。

TLDR; TLDR; I am not aware that it is possible, but all terminal shenanigans for spawning multiple background processes are possible in bash/zsh.我不知道这是可能的,但是在 bash/zsh 中,所有用于生成多个后台进程的终端恶作剧都是可能的。 See examples below, as well as links.请参阅下面的示例以及链接。

Explanation解释

It isn't possible to create a new terminal inside of Vscode from the terminal itself.无法从终端本身在 Vscode 内部创建新终端。 Same thing goes for Jupyterlab, mainly for security reasons. Jupyterlab 也是如此,主要是出于安全原因。 But it is possible to spawn another terminal as part of the original command.但是可以生成另一个终端作为原始命令的一部分。

bash -c 'some string command'

And the use of operators like & ;以及& ;等运算符的使用&& and || &&|| , which execute depending on some condition. ,它根据某些条件执行。 Mainly execute and don't wait . 以执行为主,不要等待 Execute regardless of the first command success ;不管第一个命令是否成功都执行; , and execute if successful and unsuccessful the last two. , 如果最后两个成功和不成功则执行。 ( Check more here) 在这里查看更多)

command1 & command2 

Example: echo 'something1' & echo 'something2' produces the following:示例: echo 'something1' & echo 'something2'产生以下内容:

[1] 53159
something1
something2
[1]  + done       echo 'something1'     

This can be combined with the first example: bash -c 'sleep 3; echo "something1"' & echo "something2"这可以与第一个示例结合使用: bash -c 'sleep 3; echo "something1"' & echo "something2" bash -c 'sleep 3; echo "something1"' & echo "something2" which produces: bash -c 'sleep 3; echo "something1"' & echo "something2"产生:

[1] 60427
something2
<machine_id>% something1

[1]  + done       bash -c 'sleep 3; echo "something1"'

In addition, you can probably include some waits and signal catching, but that is another can of worms.此外,您可能可以包括一些等待和信号捕获,但那是另一种蠕虫病毒。

I see the VSCode as a keyword.我将VSCode视为关键字。 Not zsh or Apple .不是zshApple

Since it's the main environment you want to work with, you should search for the answers in this environment.既然它是你要工作的主要环境,你应该在这个环境中寻找答案。

And Tasks feature is the answer: https://code.visualstudio.com/Docs/editor/tasks . Tasks功能就是答案: https://code.visualstudio.com/Docs/editor/tasks I would highly recommend getting familiar with it.我强烈建议您熟悉它。 It has way more features than I will show in this answer.它的功能比我将在这个答案中展示的要多得多。

The <task>.presentation.panel parameter is responsible for specifying a panel (ie tab): "new", "shared", "dedicated". <task>.presentation.panel参数负责指定一个面板(即选项卡):“new”、“shared”、“dedicated”。

<task>.dependsOn and <task>.dependsOrder specify dependencies and orders for them. <task>.dependsOn<task>.dependsOrder指定它们的依赖关系和顺序。

With all these parameters above you may end up with something like that in your tasks.json :使用上面的所有这些参数,您最终可能会在tasks.json中得到类似的东西:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run the first portion",
            "type": "shell",
            "command": "sleep", // Your program goes here
            "args": ["2"], // Your arguments goes here
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "new",
                "showReuseMessage": true,
                "clear": false
            },
        },
        {
            "label": "Run the second portion",
            "type": "shell",
            "command": "echo",
            "args": ["Right after the first portion"],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "new",
                "showReuseMessage": true,
                "clear": false
            },
        },
        {
            "label": "Run all portions",
            "type": "shell",
            "dependsOrder": "sequence",
            "dependsOn": [
                "Run the first portion",
                "Run the second portion",
            ],
        }
    ]
}

It gives you 3 tasks.它给你3个任务。 Run all portions is the main one. Run all portions是主要部分。 But you still will be able to execute others separately.但是你仍然可以单独处决其他人。

Note: tasks will be executed in sequence in different panels/tabs, but it's easy to control.注意:任务将在不同的面板/选项卡中按顺序执行,但很容易控制。 You just need to tune it a bit, if the first portion is not expected to be stopped or executed in a new panel all the time.如果第一部分不希望一直在新面板中停止或执行,您只需要稍微调整一下。

Note 2: Take a look into the launch.json if you need an interactive debuggers feature.注意 2:如果您需要交互式调试器功能,请查看launch.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Visual Studio Code 中的集成终端打开文件? - How to open a file from the integrated terminal in Visual Studio Code? 如何从 VSCode 中的集成终端打开文件到新选项卡 - How to open a file from the integrated terminal in VSCode to a new tab 如何在 Visual Studio Code 中从集成终端引用当前文件 - How to refer to current file from Integrated Terminal in Visual Studio Code Visual Studio Code中的键盘快捷键可打开键盘快捷方式面板 - Keyboard short cut in Visual Studio Code to open the keyboard shortcuts panel 是否可以在 Visual Studio Code 启动时自动打开集成终端? - Is it possible to have the Integrated Terminal open automatically on start of Visual Studio Code? macbook上打开Visual Studio Code集成终端的快捷方式是什么? - What is the shortcut to open the Visual Studio Code integrated terminal on macbook? 在当前项目目录(mac)中打开Visual Studio Code Integrated Terminal吗? - Visual Studio Code Integrated Terminal open at the current project directory (mac)? 用于在Visual Studio Code上打开另一个集成终端实例的命令? - Command to open another instance of integrated terminal on Visual Studio Code? Unicode字符在Visual Studio代码的集成终端中无法正确显示 - Unicode characters not displaying properly in the Visual Studio Code's integrated terminal visual studio code中隐藏集成终端的快捷方式是什么 - What's the shortcut to hide the integrated terminal in visual studio code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM