74

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?

Amanda
  • 12,099
  • 17
  • 63
  • 91
  • If you're playing around at home, it turns out that the real solution is gedit + ipython listener. Edit and run, instead of trying to compose loops in the terminal. – Amanda Sep 30 '13 at 20:11

8 Answers8

90

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

Andy K
  • 4,944
  • 10
  • 53
  • 82
JS.
  • 14,781
  • 13
  • 63
  • 75
  • This works also on Windows in Powershell using ConEmu. ^_^ – illagrenan Jul 02 '18 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 Jul 10 '18 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`. – user1717828 Mar 06 '19 at 15:15
  • i'm confused there two solutions that have similar upvotes... but use different keyboard shortcuts. this solution uses ctrl+q, ctrl+j the solution by @ShawnFumo uses ctrl+v, ctrl+j. how can there be two different keyboard shortcuts that both work? – Trevor Boyd Smith Jun 13 '19 at 12:51
  • 1
    nice find! how _did_ you come across this? – WestCoastProjects Jun 23 '19 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. Jun 23 '19 at 16:00
  • Stumbled across this _again_ 2 years later. Thanks yet again . Are we allowed to re-upvote after a specified time has elapsed? – WestCoastProjects Mar 11 '21 at 17:57
61

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
yonilevy
  • 5,320
  • 3
  • 31
  • 27
  • I did look through the documentation but didn't quite know that was what I was looking for. – Amanda Nov 23 '12 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 Sep 30 '13 at 18:55
  • 2
    For me `%edit` opened my *least* favorite editor! How can I request another? – peer Feb 27 '19 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 Feb 28 '19 at 14:09
55

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.

ShawnFumo
  • 2,108
  • 1
  • 25
  • 14
52

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.

Kamaraju Kusumanchi
  • 1,809
  • 19
  • 12
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

alpha_989
  • 4,882
  • 2
  • 37
  • 48
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?

Community
  • 1
  • 1
Emanuil Tolev
  • 315
  • 3
  • 8
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.

Robert Vanden Eynde
  • 681
  • 1
  • 7
  • 18
  • 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. – Kamaraju Kusumanchi Feb 27 '19 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!