9

In IPython (7.3.0), I can edit a multiline code block:

In [1]: def funtcion(a, b):
   ...:     return a + b
   ...:

I then realize that I've made a typo, which I want to correct (| marks the location of the cursor):

<press up arrow, then edit>
In [2]: def funct|ion(a, b):
   ...:     return a + b
   ...:

I want to execute that:

<press enter>
In [2]: def funct
   ...: |ion(a, b):
   ...:     return a + b
   ...:

...which doesn't really do what I want.

Is there a way to force IPython to execute the current cell?

1
  • go to the end of the block using the arrow keys then press enter on an empty line Commented Feb 25, 2019 at 9:55

1 Answer 1

11

As @AntiMatterDynamite has mentioned in the comments, you could navigate to the end of the block and then hit Enter. But (for me, at least) that seems to miss the point.

Instead, try hitting Shift+Enter mid line, which should execute the current cell regardless of cursor position. This has always worked for me in Spyder's IPython console.

Interestingly, when I tested hitting Shift+Enter in a regular IPython console (ie. IPython initialised from the terminal, not from Spyder) it didn't work. However, hitting Esc followed by Enter did work.

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

5 Comments

Works perfectly, thanks! I tried a lot of shortcuts but not this one!
If ESC Enter works Alt Enter may work too. Usually ESC is interchangeable with Alt but not always. In this case it worked for me, Thanks!
On a MacBook no combination of ⌘, ⌥, ⌃ or ⇧ worked, but ESC then ⏎ worked.
@geckos Interesting, I did not know that!
I have to use Alt+Enter on ubuntu.

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.