5

I wanted to use IPython interactive in VSC (not using a notebook). By using a keybiding for workbench.action.terminal.runSelectedText, I can successfully execute a selection to a python interactive shell, but with iPython it fails to run the cell and I have to change selection to the terminal and press enter.

enter image description here

Here's my keybidings.json file:

[
    {
        "key": "ctrl+enter",
        "command": "workbench.action.terminal.runSelectedText"
    }
]

Any help to solve this is much appreciated! Ideally, a keybinding configuration that includes an extra enter.

5 Answers 5

2

I have this problem when I upgraded IPython to version 7.30.1 The solution I found is that add "--simple-prompt", this is not perfect (just no color theme or auto completion), but at very least, you don't need to press enter when sending codes to run.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed it creates other nuisances, but it does solve the issue!
0

IPython is not officially supported by the Python extension and this is part of the reason: IPython's design simply requires you to press Enter an extra time based on how VS Code sends text into the terminal.

2 Comments

I see, that explains it. But I guess better way to putt what I'm looking for is a keybinding to add that Enter an extra time for me.
I don't know if you can make that work. Any keybinding to send the line to the terminal will probably execute a newline in the text buffer and not the terminal (assuming you can combine the two things in the first place).code.visualstudio.com/docs/getstarted/… is the docs for customizing keyboard shortcuts.
0

This is an issue with IPython's autoindent feature. You can disable this by passing --no-autoindent when launching IPython (i.e. ipython --no-autoindent).

edit: added gif.

enter image description here

3 Comments

It didn't work unfortunately. :/ Maybe your setup is slightly different? Are you using the same keybiding?
The keybinding shouldn't matter, it's the command itself that triggers the action. I've added screen capture using "Run Selected Text" from the command palette to demonstrate. Also note that you need highlight an empty line at the end of the code finish the statement in Ipython.
I see, but I think --no-autoindent solves a different problem than I have. What I was hoping to get is the same behavior as python interactive shell, which is to run a single line statement by default when calling runSelectedText. Even when selecting an extra blank line I still need to select the terminal and press enter.
0

I was annoyed by this quirk in vs-code too.

I noticed that when using a python virtual environment (I use pipenv) my selected line was executed in an Ipython terminal and no extra Enter key press was needed.

To reproduce or test out:

  1. Launch VS Code
  2. Open a python file and a terminal window
  3. In the terminal run pipenv shell (requires pip install pipenv && pipenv install ipython)
  4. Then in the terminal run ipython (or ipython --no-autoindent)
  5. Select a line in your python file and from a vs-code command palette execute Terminal: Run Selected Text in Active Terminal (or use a keyboard shortcut)

screenshot of python window and terminal window

Comments

0

it's a bit late, but I think I've found the solution to this annoying behavior:

  1. Install the Multi-command extension.
  2. Copy the following code to your keybindings.json file:
[
    {
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorTextFocus && !findInputFocused && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocused && editorLangId == 'python'"
    },
    {   
        // Use this when IPython gets borked using "python.execSelectionInTerminal"
        "key": "shift+enter",
        "command": "extension.multiCommand.execute",
        "args": {
            "interval": 40,
            "sequence": [
                "python.execSelectionInTerminal",
                "workbench.action.terminal.focus",
                "workbench.action.terminal.scrollToBottom",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" } // This basically "presses enter"
                },
                "workbench.action.focusActiveEditorGroup"
            ]
        },
        "when": "editorTextFocus && !findInputFocused && !replaceInputFocused && editorLangId == 'python'"
    }
]
  1. Restart the Vscode terminal.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.