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

4

我在网上没有找到答案,所以来这里问一下。 我是Visual Studio Code的开发人员,比如说Python语言。 如果我运行代码,它总是会打开一个新的终端,这样我就可以快速打开很多终端,但每次关闭它们都很麻烦。 有可能在当前已打开的终端中运行代码而不需要自己输入“python文件名”吗?


你应该可以创建一个 tasks.json 文件,使得 VSCode 可以在 shell 中运行 Python 任务,不是吗? - Spencer Pollock
2个回答

3
为了在VSCode中作为任务运行项目,您只需创建一个像这样的.vscode/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 shell command",
            "type": "shell",
            "command": "echo 'Hello world!'",
            "group": "test",
            "presentation": {
                "panel": "shared", 
                "reveal": "always",
                "focus": true
            },
        }
    ]
}

关键是要在同一终端中运行的重点是 "panel": "shared" 行。这将在同一终端中运行命令。
注意:由于我目前无法访问VSCode实例,因此此任务未经测试。 这里有更多关于VSCode任务的信息。 这是VSCode的Python任务教程。 有关任务输出行为的更多信息。

-2

这是我的任务文件,供任何想更轻松地启动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
            }
        }
    ]
}

使用快捷键CTRL+SHIFT+B来构建任务,当您想重新构建时,请使用相同的快捷键 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接