Ipython: initiate multiline input in terminal ipython and ipython console

Created on 15 Oct 2012  ·  14Comments  ·  Source: ipython/ipython

In qtconsole (via ctrl-enter), and in the notebook (via just enter within a cell), you can turn any one line input into a multiline input. It would be nice to get something like that going for the terminal and console entry points.

Currently, a potential workaround is to use and if 1: for the first line of input, which then initiates multiline input in all clients, but that seems a bit awkward.

In general, where possible, I think it makes sense to have feature parity across the different clients.

Concrete example: I can enter the following input using the qtconsole by pressing ctrl-enter at the end of each line:

In [2]: print "hello"
   ...: print "this is all one input"
   ...: print "same here..."
   ...: 

and if I execute that line, and then start up a terminal based ipython client and press the up arrow, I'll see an appropriate prompt:

In [1]: print "hello"
print "this is all one input"
print "same here..."

which can be executed and run, but there's no way to enter a freeform multiline input like that from the terminal, which makes for an awkward workflow.

thanks to @michaelpacer for bringing this to my attention.

Most helpful comment

Pressing ctrl+o works for me (tried in cmd and conemu consoles).

Also possible to use (

 In [84]: (
     ...: fig, ax = plt.subplots(1,2)
     ...: ax[0].plot([1,2,3])
     ...: ax[1].plot([3,2,1])
     ...: fig.show()
     ...:

When you're done typing, just return to the first line and remove the (.

All 14 comments

I think we run up against the constraints of the terminal/readline here. As far as I know, there's no easy way to detect ctrl-enter. We just call raw_input() and get a string returned - or an exception raised if the user presses Ctrl-D or Ctrl-C.

As a longer term project, it might be possible to do this by using a library like pyrepl instead of readline. This is something that we've been interested in for a while, but no-one's got around to investigating it closely. Another way would be to build a new frontend using curses or a similar tool like urwid.

One thing you can do is hit ctrl-v, ctrl-j to enter a newline without running the input. I think this is part of Bash itself since it also works on the terminal (ctrl-v says to treat the next hotkey as a control sequence and ctrl-j is newline). I haven't had a chance to mess with iTerm2 on this, but maybe it is possible to map ctrl-enter to that double-sequence somehow?

Edit: You can indeed replicate this in iTerm2 at least. Can go to your global shortcut keys and set ctrl-enter to "Send hex codes" and then: 0x16 0x0a

Would it maybe be possible to get this type of functionality now with prompt_toolkit?

Yes, although not with an enter shortcut - it seems the only combo with enter we can detect with enter is Alt-Enter (or Esc, Enter), and we're already using that for force execution (i.e. don't add a newline). We could do this with another shortcut, though - or you could add a custom shortcut.

One way you can sort of do this in current IPython is to press F2, which will drop you into an editor, and put the contents into your input when you're done. You can control what editor it opens by setting the $EDITOR environment variable.

Alt-Enter on Windows changes ipython windows to full screen.

Using the latest ipython and prompt_toolkit, and if you have vi-mode enabled, you can type in your command, then press ESC, then press o. This will create a new line, just like in vi.

By this way you can have multiline commands.

In [29] : %reset -s #----> press `ESC` then `o`
            %run foo.py

Ref

  1. Enabling Vi mode in ipython: http://koo.fi/blog/2016/08/04/enable-vi-editing-mode-in-ipython-5/

Just ran into this. I'm positive ctrl-enter used to create a new line!?! But this issue would suggest I'm mistaken :(

Another option is to interpret semicolon as a line separator. e.g.

%run slow_script.py; !display_notification.sh

will run my python script, then execute some bash script.

Edit: this doesn't actually work, it's just a suggestion

I do it using ;

cv2.imshow('image', im) ;
cv2.waitKey(0) ;
cv2.destroyAllWindows()

Pressing ctrl+o works for me (tried in cmd and conemu consoles).

Also possible to use (

 In [84]: (
     ...: fig, ax = plt.subplots(1,2)
     ...: ax[0].plot([1,2,3])
     ...: ax[1].plot([3,2,1])
     ...: fig.show()
     ...:

When you're done typing, just return to the first line and remove the (.

I do it using ;

cv2.imshow('image', im) ;
cv2.waitKey(0) ;
cv2.destroyAllWindows()

It doesn't seem like you can mix python and magics this way:

In [5]: %run test.py ;\
   ...: !echo foo

hi

Pressing ctrl+o works for me (tried in cmd and conemu consoles).

For me it's alt+o.

For me it's ctrl+o

Ctrl+o does works for me in ipython, but not jupyter console.

Ctrl+O works, this issue can be closed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  4Comments

hexhexd picture hexhexd  ·  4Comments

jakirkham picture jakirkham  ·  4Comments

ipython picture ipython  ·  3Comments

sataliulan picture sataliulan  ·  4Comments