简体   繁体   中英

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. I am thinking AppleScript is the way to go, but if there's another way to run from ZSH I'd be thrilled.

TLDR; I am not aware that it is possible, but all terminal shenanigans for spawning multiple background processes are possible in 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. Same thing goes for Jupyterlab, mainly for security reasons. 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:

[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" which produces:

[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. Not zsh or Apple .

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 . 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>.dependsOn and <task>.dependsOrder specify dependencies and orders for them.

With all these parameters above you may end up with something like that in your 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. Run all portions is the main one. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM