76

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?

2

8 Answers 8

95

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

8
  • This works also on Windows in Powershell using ConEmu. ^_^
    – illagrenan
    Commented Jul 2, 2018 at 20:08
  • 5
    This worked for me, other answers with ctrl-v did not work for me but ctrl-q let me put in the linebreak
    – Eric Blum
    Commented Jul 10, 2018 at 20:07
  • 2
    Note: you don't have to let go of Ctrl for this to work: you can holding Ctrl and press q then j. Commented Mar 6, 2019 at 15:15
  • 1
    nice find! how did you come across this? Commented Jun 23, 2019 at 14:08
  • 1
    @javadba: That combination is used to add linebreaks in Emac's minibuffer. I took a chance that it might work for IPython.
    – JS.
    Commented Jun 23, 2019 at 16:00
63

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
4
  • I did look through the documentation but didn't quite know that was what I was looking for.
    – Amanda
    Commented Nov 23, 2012 at 22:43
  • 7
    For those of us newer to iPython, just wanted to also point out that %edit _2 would re-open the editing session in the example above and %edit -p always opens your last editing session.
    – ShawnFumo
    Commented Sep 30, 2013 at 18:55
  • 2
    For me %edit opened my least favorite editor! How can I request another?
    – peer
    Commented Feb 27, 2019 at 22:49
  • 1
    @peer %edit uses the environment variable EDITOR, which is used by many command-line utilities when in need of opening an editor. For example, if you run EDITOR=vim ipython - vim will be used. You could set it to a different value in your bashrc/zshrc/etc. file so it will always be used.
    – yonilevy
    Commented Feb 28, 2019 at 14:09
57

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.

2
  • 6
    You can also use ctrl-v, ctrl-i to add a tab.
    – Ara
    Commented May 4, 2014 at 12:15
  • 1
    Or ctrl-v, tab to add a tab
    – Samizdis
    Commented Apr 15, 2016 at 12:55
53

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.

9

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

6

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. How to get an ipython graphical console on Windows 7?

5

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.

1
  • 2
    The Ctrl+q Enter shortcut is not working with IPython 7.3.0 but works with IPython 5.3.0. I am using git bash 2.18.0 + Windows 7. One alternative is to use Ctrl+on which works well in both IPython versions. Commented Feb 27, 2019 at 20:21
0

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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.