27

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,

3

4 Answers 4

23

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"
}
3
  • Yes, this works! I guess to truly work as elpy. I guess I need to add multiple commands using tasks. "when": "editorTextFocus && !editorHasSelection" "commands": "expandLineSelection" & "workbench.action.terminal.runSelectedText" & "cancelSection" but this is really good. Thank you Aug 14, 2017 at 14:58
  • I just posted Answer to my above added comment Q extended. Aug 14, 2017 at 15:40
  • 4
    Surprising it doesnt have a run current line and move cursor to next short cut(Like f9 in spyder) . Thought its very commonly used shortcut
    – sjd
    Dec 9, 2020 at 8:45
17

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"}
4
  • 3
    It results in runing the next line when the mouse focus is in a line, not the current line. selection works. Feb 15, 2019 at 8:31
  • Is there a way to get it to insert a new line afterwards if it's the last line in the file?
    – Allen Wang
    Mar 21, 2019 at 21:26
  • @ZhilongJia I edited the answer to add cursorUp to the beginning of the macro. It works but I don't understand why.
    – coletl
    May 4, 2022 at 21:46
  • Never mind, the macro runs the next line (when no selection) as soon as you try to run an empty line.
    – coletl
    May 5, 2022 at 16:22
13

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

1
  • If you click on that settings icon at the right end of that row, you can edit the 'keybinding', which is the shortcut. In my Ubuntu VSCode there was no default shortcut, so I added it this way.
    – mikey
    Oct 12 at 9:58
1

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'"
}, 

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.