【问题标题】:Visual Studio Code run code in the same terminalVisual Studio Code 在同一终端中运行代码
【发布时间】:2019-08-03 03:22:00
【问题描述】:

我在互联网上没有找到答案,所以我在这里问。 我实际上是 Visual Studio Code 的开发者,例如 Python 之类的语言。 如果我正在运行代码,它总是会打开一个新终端,这样我就可以更快地打开很多终端,每次关闭它们都很痛苦。 可以在当前打开的终端中运行代码而不用写我自己的“python文件名”吗?

【问题讨论】:

  • 难道不能为 VSCode 创建一个tasks.json 文件以在 shell 中运行 Python 任务吗?

标签: python terminal visual-studio-code


【解决方案1】:

要在 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 实例。

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

【讨论】:

    【解决方案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 来构建任务,当你想要重新构建任务时使用同样的快捷键:)

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多