I'm using VSCode with a keybinding that sends a command to the terminal, where IPython is running (not Jupyter Notebook, just plain IPython in the terminal).
I want to select a line of code (e.g. checkpoint_paste()) and press a shortcut key to send and execute it in the IPython terminal.
Here is how I’ve set things up:
settings.json
"multiCommand.commands": [
{
"command": "extension.multiCommand.pasteToTerminal",
"sequence": [
"editor.action.clipboardCopyAction",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "checkpoint_paste()\u000D"
}
}
]
}
]
keybindings.json
{
"key": "ctrl+shift+r",
"command": "extension.multiCommand.pasteToTerminal",
"when": "editorHasSelection"
}
Now, when I select code in VSCode and press the shortcut, the IPython terminal shows:
In [1]: checkpoint_paste()
...:
I understand that \u000D simply creates a newline, but does not execute the command in IPython.
How can I actually execute the command in IPython from a VSCode shortcut?
I've spent the past two days searching for solutions, but haven't found anything that works.
Any help would be greatly appreciated!
Other things I’ve tried:
"text": "checkpoint_paste()\u000D\u000D"
"text": "checkpoint_paste()\n"
"text": "checkpoint_paste()\r\n"
"python.terminal.launchArgs": [
"-m",
"IPython",
"--no-autoindent"
]
1 Answer 1
I ran into the same issue and finally found a setup that works the way I expected. Basically, I wanted to send code from the editor to an already running IPython terminal in VS Code (not launch a new one every time), and have it actually run automatically, without me having to press Enter again.
Here’s how I got it working:
1. Set IPython as the default terminal
First, go to your settings.json and add this:
"python.terminal.launchArgs": [
"-m",
"IPython",
"--no-autoindent"
]
That makes sure VS Code opens IPython instead of the plain Python REPL, and the --no-autoindent flag avoids the annoying "extra Enter" issue when sending code.
2. Automatically press Enter after sending code
By default, when you press Shift+Enter (to run selected code), VS Code sends it to the terminal, but doesn’t execute it—you still have to manually hit Enter in the terminal. Kind of defeats the point.
To fix that, I installed the Multi Command extension and updated my keybindings.json like this:
// Remove the default Shift+Enter
{
"key": "shift+enter",
"command": "-python.execSelectionInTerminal",
"when": "editorTextFocus && !findInputFocused && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocused && editorLangId == 'python'"
},
// Add a custom one that also presses Enter
{
"key": "shift+enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"python.execSelectionInTerminal",
"workbench.action.terminal.focus",
{
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u000D" } // sends Enter
},
"workbench.action.focusActiveEditorGroup"
]
},
"when": "editorTextFocus && !findInputFocused && !replaceInputFocused && editorLangId == 'python'"
}
Now when I press Shift+Enter, it sends the code to the IPython terminal, executes it right away, and then puts my cursor back in the editor. Works exactly how I wanted.
Hope that helps someone—this setup took me way longer to figure out than it should have 😅
Comments
Explore related questions
See similar questions with these tags.