26

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,

user6273920
  • 713
  • 1
  • 7
  • 16
  • 2
    Take a look at https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner – Aluan Haddad Aug 14 '17 at 04:10
  • Aluan: Needs a bit of tweaking at least, but looks promising. At least I should be able to utilize it as a sample. Thanks much, – user6273920 Aug 14 '17 at 08:59
  • Does this answer your question? [How can I run text selected in the active editor in VS Code's integrated terminal?](https://stackoverflow.com/questions/38952053/how-can-i-run-text-selected-in-the-active-editor-in-vs-codes-integrated-termina) – starball Aug 31 '23 at 18:22

4 Answers4

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"
}
kwood
  • 9,854
  • 2
  • 28
  • 32
  • 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 – user6273920 Aug 14 '17 at 14:58
  • I just posted Answer to my above added comment Q extended. – user6273920 Aug 14 '17 at 15:40
  • 3
    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 09 '20 at 08: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"}
coletl
  • 803
  • 5
  • 17
user6273920
  • 713
  • 1
  • 7
  • 16
  • 3
    It results in runing the next line when the mouse focus is in a line, not the current line. selection works. – Zhilong Jia Feb 15 '19 at 08: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 '19 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 04 '22 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 05 '22 at 16:22
12

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

Daniel Argüelles
  • 2,229
  • 1
  • 33
  • 56
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'"
}, 
bilibili
  • 11
  • 2