Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code execute current line or selection to in the integrated console

This old Emacs user, who is used to elpy, is attempting to move onto VSCode with Scala & more specifically Ammonite repl.

I used Ctrl+' to open the integrated terminal & all I have to do is type amm on the bash shell (ubuntu) to open the repl; however, I still miss being able to send the either the line or selection from the editor to integrated shell with Ctrl+Enter.

I guess this means a bit of coding. Where can I start? Has anyone accomplished similar?

Thanks much,

like image 887
user6273920 Avatar asked Aug 14 '17 03:08

user6273920


4 Answers

If you already have your terminal and REPL open, there is a built in command called "Run Selected Text in Active Terminal" / workbench.action.terminal.runSelectedText.

It has no default keybinding, so you need to set it yourself. Something like this would work:

{
  "key": "ctrl+enter",
  "command": "workbench.action.terminal.runSelectedText",
  "when": "editorTextFocus && editorHasSelection"
}
like image 110
kwood Avatar answered Oct 17 '22 19:10

kwood


Actually, I found that adding VSCode Macros extension does the job:

I just changed settings.json:

{
    "window.zoomLevel": 1,
    "editor.fontSize": 11,
    "terminal.integrated.fontSize": 11,
    "macros": {
        "execCurLn": [
            "cursorUp",
            "expandLineSelection",
            "workbench.action.terminal.runSelectedText",
            "cancelSelection"
        ]
    }
}

and added (1st part is pure @kwood & thank u again) to keybindings.json

   {
        "key": "ctrl+enter",
          "command": "workbench.action.terminal.runSelectedText",
            "when": "editorTextFocus && editorHasSelection"
    }
    {
        "key": "ctrl+enter",
          "command": "macros.execCurLn",
            "when": "editorTextFocus && !editorHasSelection"
    },
{ "key": "ctrl+`", "command": "workbench.action.terminal.focus"},
{ "key": "ctrl+`", "command": "workbench.action.focusActiveEditorGroup", "when": "terminalFocus"}
like image 42
user6273920 Avatar answered Oct 17 '22 18:10

user6273920


Open the command palette with CTRL+SHIFT+P and look for Terminal: Run Selected Text In Active Terminal. On the left you will see the key binding or a wheel engine to set the binding.

enter image description here

like image 9
Daniel Argüelles Avatar answered Oct 17 '22 19:10

Daniel Argüelles


follow another post VS Code move to next line on run ctrl + enter, to run current line then cursor down, avoiding running next line unexpectedly

in settings.json, add

"macros": {
    "pythonExecSelectionAndCursorDown": [
        "python.execSelectionInTerminal",
        "cursorDown",
    ]
} 

in keybindings.json, add

{
    "key": "ctrl+enter",
      "command": "macros.pythonExecSelectionAndCursorDown",
        "when": "editorTextFocus && editorLangId == 'python'"
}, 
like image 1
bilibili Avatar answered Oct 17 '22 19:10

bilibili