2

I am using VS Code with multiple editors and multiple terminals. I ideally would like to run code in the terminal that is currently visible. Using the macros library by geddski I came close to a solution but could not figure out how to finalize the process.

settings.json

"macros": {  // Note: this requires macros extension by publisher:"geddski" 
"runSelectedInFocusedTerminal": [
"editor.action.clipboardCopyAction",
"workbench.action.terminal.focus",
"workbench.action.terminal.paste",
"workbench.action.focusActiveEditorGroup",
"cursorDown"
]
}

keybindings.json

{
    "key": "ctrl+]",
    "command": "macros.runSelectedInFocusedTerminal",
    "when": "editorTextFocus && editorLangId == 'python' && resourceExtname == '.py'"
}

This macro successfully copies the line or the selected text onto the terminal but it does not run it and go back to the visible editor as I would like.

I also have the ctrl+enter trick from this post: this SO post. However this runs the code in the first terminal I opened using this shortcut and not the visible terminal so it is not a viable solution for using terminals effectively on a multi terminal setup.

starball
  • 20,030
  • 7
  • 43
  • 238
Kaan Yolsever
  • 242
  • 2
  • 13

2 Answers2

0

Can you not just use the Terminal: Run Selected Text in Active Terminal command in the command palette or bind it to a keybinding? Ex. in keybindings.json:

{
    "key": "", // TODO
    "command": "workbench.action.terminal.runSelectedText"
}

Also note that VS Code 1.77 added the runCommands command for keyboard shortcuts, so you shouldn't need an extension to bind a sequence of commands to a keyboard shortcut anymore.

starball
  • 20,030
  • 7
  • 43
  • 238
0

Consider using 'sendSequence' instead of executing pasted code in terminal.

"macros": {
    "runSelectedInFocusedTerminal": [
        "editor.action.clipboardCopyAction",
        "workbench.action.terminal.focus",
        {
            "command": "workbench.action.terminal.sendSequence",
            "args": {
                "text": "${clipboard}\n"
            }
        },
        "workbench.action.focusActiveEditorGroup",
        "cursorDown"
    ]
}
Baris Taskiran
  • 130
  • 1
  • 8