简体   繁体   English

Visual Studio Code在同一终端中运行代码

[英]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. 我实际上是在Visual Studio Code上开发某种语言,例如Python。 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" ? 可以在打开的当前终端中运行代码,而无需编写我自己的“ python文件名”?

To run a project in VSCode as a task, all you need to do is create a .vscode/tasks.json file like so. 要将VSCode中的项目作为任务运行,您需要做的就是像这样创建.vscode/tasks.json文件。 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. 在同一终端上运行的主要焦点是"panel": "shared"行。 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. 注意:此任务未经测试,因为我目前无法访问VSCode实例。

Here is some more information on VSCode tasks. 这是有关VSCode任务的更多信息。 Here is a Python task tutorial by VSCode. 这是VSCode的Python任务教程。 More information on task output behaviour. 有关任务输出行为的更多信息。

Here my task file for anyone wants to launch a python file easier : 在这里,我的任务文件供任何想要更轻松地启动python文件的人使用:

{
    // 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 :) 使用快捷键CTRL + SHIFT + B生成任务,并在要重建时执行相同的快捷键:)

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

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