29

I am reading the book Python Data Science Handbook by Jake VanderPlas. In the first chapter a multi line input in IPython is illustrated:

multiLineInput

Can anybody please tell me how to do that? I know how to write a block before executing it in Jupyter Notebook, but in the IPython shell I don't know how to do it.

0

7 Answers 7

68

Ctrl+o

If you type Ctrl+o, you should be able to add additional lines to the input to run them simultaneously.

If that doesn't work, as a workaround, you can paste multiple lines after copying them from elsewhere, or you can press enter on a line with a semicolon or unclosed parentheses. IPython will automatically give you another line to finish your statement rather than running and instantly hitting a syntax error.

Depending on the environment where you are running IPython, you might also want to try Ctrl+Enter or Shift+Enter.

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

2 Comments

I am using in Pycharm and that does not work, however Ctrl + Enter does the job
(maOS Ventura, launching iPython 8.3 via Terminal from Bash prompt) Ctrl+O creates a new line just below the line where Ctrl+O is pressed and one needs to move the cursor using the down arrow key to the beginning of the newly created line. Wish there was a shortcut that does both.
3

Use 'shift' + 'Enter' at the end of line to avoid execution and keep typing your code. When you are done writing your block of code hit 'Enter' Have a look to the following picture If for some reason shift doesn't work, Ctrl + Enter instead. I hope this helps, good luck.

2 Comments

Thank you @ZAnteneh. The problem is that the author uses jupyter notebook, but that was not very clear for me...anyway, to go to a new line without executing, ctrl + o works fine for me (working on MacOS).
Ctrl + O is the one which works in ubuntu - ipython v5.5.0
3

As an addition to the previous answers. you will need to press Enter twice to run the code. In Python 3.7.3 and maybe above, pressing enter just takes you to a new line of the multi-line ipython input. In order to run the code, press Enter again.

1 Comment

one enter on an empty line at the end ran my block
3

Add "<SPACE>\" at the end of the first line before hitting <ENTER>.

It allows you to enter more lines and you can remove the " \" while editing the multiline-input as well. Execute the input as described before by pressing <ENTER> in the last (empty) line, which may require pressing <ENTER> 2x at the end of the last non-empty line.

If the input already executes while pressing <ENTER> anywhere in the last non-empty line of the input you can still insert lines anywhere by pressing <ENTER> at the end of the line above and you can add a line at the end by adding "<SPACE>\" at the end of the last line as before.

For some reason none of the above methods worked for me in a cmd shell on Windows, but this did the trick.

1 Comment

i suffered of the need to do this, ctrl+o made full block editing possible.
2

Use Ctrl + "O" before hitting Enter

I had tried the same for above code.

Comments

-1

i tried all the solutions above under linux, and the only one that worked for me is ctrl+o.

a new line of input is being created, though the cursor didn't move to the beginning of the newly created line.

the up and down arrows move inside of the input normally, but up on the first line loads the previous input for editing, and down on the last one loads the next.

Comments

-3

My suggestion is that rather using ipython shell, install jupyter notebook (pip install jupyter). then type jupyter notebook in the terminal rather than typing ipython. this command will open jupyter notebook in your default browser. here you can do whatever you want to do. you don't need have any internet connection. to stop the jupyter server, type ctrl+d in the terminal. you can watch the following tutorial on youtube: https://youtu.be/DKiI6NfSIe8

Enjoy.

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.