Adding line breaks in ipython

PythonIpython

Python Problem Overview


If introduce a for loop in iPython, or any multi-line command, how do I go back and add lines to it? I ran this:

for row in table.find_all('tr'):
    cells = row.find_all('td')
    for c,cell in enumerate(cells):
        print c,":",cell.get_text().strip()
    try:
        this = cells[0]
        that = cells[1]
        the_docket = cells[2]
        other_thign = cells[3]
        jumble = re.sub('\s+',' ',str(cells[5])).strip()            
    except:
	    "Nope"

And realized I need to add a line to it, but I can't just hit "enter" in iPython, b/c that runs the command. So can I edit that multi-line command w/in iPython?

Python Solutions


Solution 1 - Python

Been suffering this problem for a while. I just found that when using Ctrl-qCtrl-j (That's lowercase Q, J, no need to hold the shift key) will add a linefeed to an existing IPython edit session.

for li in some_list: print(li)    

Moving the cursor after the colon and pressing Ctrl-qCtrl-j

for li in some_list:
print(li)

IPython: 5.2.1, iTerm2: 3.0.15, macOS: 10.12.6

Solution 2 - Python

The %edit magic function in iPython lets you edit code in your favorite editor and will then execute it as if it was typed directly. You can also edit code you've already typed into the repl since it's stored in a special variable, for example:

In [1]: def foo(x):
   ...:     print x
   ...:     
In [2]: %edit _i1

Solution 3 - Python

There is also a way to add a newline directly in the repl: ctrl-v, ctrl-j

The ctrl-v basically lets you send a control code and then the ctrl-j is the code for a newline (line-feed). It's a bit awkward to type but has the advantage of also working in the regular Python shell as well as in Bash itself.

Edit: At least in iTerm2, you can assign it to a single hotkey as well. I set ctrl-enter to "Send hex codes" of 0x16 0x0a. Could also use cmd-enter or whatever else.

Solution 4 - Python

You can use ctrl+on (i.e. press the control button and enter the characters 'o', 'n' while holding it). You can also do it in two steps - ctrl+o ctrl+n but I find the former easier.

  • ctrl-o - enter the multiline mode
  • ctrl-n - access command history going forward. But since there is no forward history, cursor just moves to the next line.

I verified that it works with both IPython 5.3.0 and IPython 7.3.0 on a machine running git bash 2.18.0 + windows 7.

Solution 5 - Python

A easy way of doing it is using the ;\ operator. ; to signal that its the end of a command and \ to indicate the the command follows in a new line as follows:

In[4]: password = "123";\
       username = "alpha"

This will let you have multiple line commands in ipython without invoking the editor

Solution 6 - Python

For completeness: newer IPython versions (0.11+) have a very nice graphical console which allows you to navigate your code with the arrows and even reposition the cursor with the mouse.

In multi-line statements, some keys take on a special function (arrows to navigate, Enter to insert a line break and others). You'll have to position the cursor at the end of the last line of the statement in order to get default behaviour, e.g. get the Up arrow ā†‘ to mean "previous statement" instead of "move the cursor to the previous line". Or get Enter to mean "execute code" instead of "insert line break in the middle of code".

The documentation on it is a bit sparse and fragmented in different pages, so here are the essential three links for getting started with it:

  1. Intro to the Qt Console

  2. Configuring IPython using nice per-user profile files instead of command line arguments

    You are interested in the ipython_qtconsole_config.py

  3. https://stackoverflow.com/questions/14914950/how-to-get-an-ipython-graphical-console-on-windows-7

Solution 7 - Python

Type Ctrl+q then Enter. As other pointed out, Ctrl+q lets you send a character code, but hitting Ctrl+q then Enter may be more natural than Ctrl+q then Ctrl+j.

Another option is to type ( then Enter, write, and delete the ( afterwards. This solution is similar to the one where you to type ;\ to say the statement is not completed.

Solution 8 - Python

Bit late to the party! But I noticed many people are talking about using Ctrl combinations to add extra lines. This didn't work for me until I saw a comment about it being the Emacs binding. I have set my in line editor to be Vi, if you have too then pressing Esc to go into "Normal" mode on the line before you want to add an extra line then press o (or O if you are after the line). Just like in normal Vi(m). Works for me!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmandaView Question on Stackoverflow
Solution 1 - PythonJS.View Answer on Stackoverflow
Solution 2 - PythonyonilevyView Answer on Stackoverflow
Solution 3 - PythonShawnFumoView Answer on Stackoverflow
Solution 4 - PythonKamaraju KusumanchiView Answer on Stackoverflow
Solution 5 - Pythonalpha_989View Answer on Stackoverflow
Solution 6 - PythonEmanuil TolevView Answer on Stackoverflow
Solution 7 - PythonRobert Vanden EyndeView Answer on Stackoverflow
Solution 8 - PythonherjazzView Answer on Stackoverflow