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
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.
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 modectrl-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.
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
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.
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
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