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?
Been suffering this problem for a while. Just found that Cntrl-qCtrl-j (That's lowercase Q, J, no need to hold the shift key) will add a linefeed to an existing IPython edit session.
Moving the cursor after the colon and pressing Ctrl-qCtrl-j
IPython: 5.2.1, iTerm2: 3.0.15, macOS: 10.12.6
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: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.
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:This will let you have multiple line commands in ipython without invoking the editor
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:
Configuring IPython using nice per-user profile files instead of command line arguments
You are interested in the
ipython_qtconsole_config.py
How to get an ipython graphical console on Windows 7?
Type
Ctrl+q
thenEnter
. As other pointed out,Ctrl+q
lets you send a character code, but hittingCtrl+q
thenEnter
may be more natural thanCtrl+q
thenCtrl+j
.Another option is to type
(
thenEnter
, write, and delete the(
afterwards. This solution is similar to the one where you to type;\
to say the statement is not completed.