简体   繁体   中英

Visual Studio Code run code in the same terminal

I didn't find the answer on Internet so I'm asking here. I'm actually dev on Visual Studio Code some language like Python for example. If I'm running the code, it's always opening a new terminal so I can have faster a lot of terminal opened and it's a pain to close them every time. It is possible to run the code in the current terminal opened without write my self "python filename" ?

To run a project in VSCode as a task, all you need to do is create a .vscode/tasks.json file like so. The following task will open in the integrated terminal:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run shell command",
            "type": "shell",
            "command": "echo 'Hello world!'",
            "group": "test",
            "presentation": {
                "panel": "shared", 
                "reveal": "always",
                "focus": true
            },
        }
    ]
}

Key focus to running in the same terminal is the "panel": "shared" line. This runs the command in the same terminal.

Note: this task is untested as I do not have access to a VSCode instance at the moment.

Here is some more information on VSCode tasks. Here is a Python task tutorial by VSCode. More information on task output behaviour.

Here my task file for anyone wants to launch a python file easier :

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run Script",
            "command": "python",
            "presentation": {
                "panel": "shared",
                "reveal": "always",
                "focus": true
            },
            "args": [
                "${file}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Use the shortcut CTRL+SHIFT+B for build the task and do the same shortcut when you want to rebuild :)

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