【问题标题】:Run Code on integrated terminal Visual Studio Code在集成终端 Visual Studio Code 上运行代码
【发布时间】:2017-01-31 09:58:33
【问题描述】:

我的 task.json 中有以下代码:

    {
        "taskName": "Run",
        "suppressTaskName": true,
        "args": [
            "${workspaceRoot}/bin/Albedo"
        ]
    }

当我运行“运行”时,程序按预期启动,但是当我在代码中有getchar() 时,当我的程序卡在输出中时,什么也没有发生。没有地方可以按Enter 继续。

所以我在考虑如何打开集成终端并从那里运行它?是否可以从task.json调用这样的命令?

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    此功能实际上是在即将发布的 1 月版本中添加的,请参阅发行说明草稿的 this section。本质上,您只需将其添加到您的tasks.json

    "_runner": "terminal"
    

    如果您不想等到发布(应该很快)再查看,您可以使用Insider's build

    【讨论】:

    【解决方案2】:

    要在集成终端中以交互方式运行代码,一次一行或多个选定行,只需按 ctrl+enter,我执行了以下操作:

    1. 安装macros 扩展。

    2. 将以下内容添加到User Settings

    "macros": {
        "canCopyEmpty": [
            "expandLineSelection",
            "editor.action.clipboardCopyAction",
            "cancelSelection"
        ],
        "runLine": [
            "macros.canCopyEmpty",
            "workbench.action.terminal.paste",
            {
                "command": "workbench.action.focusActiveEditorGroup",
                "when": "terminalFocus"
            }
        ],
        "runSelection": [
            "workbench.action.terminal.runSelectedText",
            "cursorDown"
        ]
    }
    
    1. 将以下内容添加到keybindings.json
    {
        "key": "ctrl+enter",
        "command": "macros.runLine",
        "args": {
            "cmd": "ls",
            "match": ".*"
        },
        "when": "editorTextFocus && !editorHasSelection"
    },
    {
        "key": "ctrl+enter",
        "command": "macros.runSelection",
        "args": {
            "cmd": "ls",
            "match": ".*"
        },
        "when": "editorTextFocus && editorHasSelection"
    }
    

    【讨论】:

    • 这是一个旧答案,对 VSCode 核心的更改破坏了原始答案中的键绑定。我已经用keybindingsuser settings 更新了它,它们适用于最新版本的VSCode。没有什么可以阻止未来的更改再次破坏它。
    猜你喜欢
    • 2021-05-26
    • 2019-08-03
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2021-11-13
    相关资源
    最近更新 更多