El_Loco
El_Loco

Reputation: 1866

Run Code on integrated terminal Visual Studio Code

I have the following piece of code in my task.json:

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

When I run "Run" the program starts as it is supposed to, but when I have getchar() in the code nothing happens when I the program it get stuck in the output. There is no place where I can press Enter to continue.

So I am thinking about how do I open the integrated terminal and run it from there? Would it be possible to call such a command from task.json?

Upvotes: 2

Views: 5389

Answers (2)

Clay
Clay

Reputation: 2726

To interactively run code in the integrated terminal, either a single line at a time or multiple selected lines by simply pressing ctrl+enter, I did the following:

  1. Install macros extension.

  2. Add the following to 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. Add the following to 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"
}

Upvotes: 1

Gama11
Gama11

Reputation: 34138

This feature was actually added in the upcoming January release, see this section of the release notes draft. Essentially, you just have to add this to your tasks.json:

"_runner": "terminal"

If you don't want to wait until release (which should be soon) to check this out, you can use an Insider's build.

Upvotes: 1

Related Questions