ipython
ipython copied to clipboard
A shortcut to "Replay History" in iPython
Sometimes, several lines of history need to be replayed while using iPython. Currently, one would search the history for the first line (probably using ^r or similar), execute it, search for the second line, execute it, and so forth.
bash shell has a nice solution: from Learning the bash shell
CTRL-O is useful for repeating a sequence of commands you have already entered. Just go back to the first command in sequence and press CTRL-O instead of RETURN. This will execute the command and bring up the next command in the history file. Press CTRL-O again to enter this command and bring up the next one.
As my usual debugging session looks like import mycode - call a function - debug - edit code (outside iPython) - start all over again, I would love to see this functionality implemented in iPython.
One way you can approach this at present is the %rerun magic. For instance, %rerun ~1/1-6 means 'run inputs 1-6 from the previous session'.
I know about this, but I've never used it. First of all, I'm bad at remembering numbers. Second, I'm mostly using one session (that may last several days). Third, I need the "latest" (i.e most recent) edition and not the original. But mainly, I like to see what's about to be executed before I execute it.
In bash they also have e.g. !316 meaning "rerun the command number 316 in history" and I've never used that either.
You can also do %hist -ng foo to search the history and get the numbers, and %recall 316-321 to bring a bunch of lines to the prompt so you can see them before running them.
Most of the bash keyboard shortcuts are actually readline keyboard shortcuts, and IPython up to version 4 also uses readline, so the same shortcut may actually work in released versions of IPython (or it may be possible to configure it using ~/.inputrc). For IPython 5, we're changing to a more powerful library called prompt_toolkit. This probably won't have Ctrl-O by default, but it should be possible to configure it.
Well here's my usual workflow:
In [3]: from my_module import * # iPython rereads the files on disk
In [4]: v = my_obj(param1='test', param2=10, param3=None)
In [5]: v.do_something()
Some debugging with pdb, ^C is pressed, pages of output.
Now I have a choice of the following:
Option A
up-arrow, up-arrow, up-arrow, (from... import ...) Enter.
up-arrow, up-arrow, up-arrow, (v = ...) Enter.
up-arrow, up-arrow, up-arrow, (v.do_...) Enter.
Option B
%hist Enter
Check and remember lines numbers
%recall 316-319 Enter
Visually check and possibly edit the bunch (lots of character movement)
Enter
I think it can be seen that Option A is more reasonable choice at this stage from the pure keypresses point of view.
My suggestion (with Ctrl-O):
up-arrow, up-arrow, up-arrow, (from... import ...) Ctrl-O
(v = ...) Ctrl-O.
(v.do_...) Ctrl-O.
(from... import ...)
At this stage, I can press Ctrl-O to re-run the cycle again, or Ctrl-C if I'm not interested.
This actually saves a lot of keystrokes.
I'm using iPython on Mac and it's my understanding that GNU readline is not used on OS X, but maybe I'm wrong on this and iPython still installs it.
IPython 4.x does depend on the gnureadline package (on Mac).
I entered "\C-o": operate-and-get-next into my ~/.inputrc, but it didn't yield the desired result.
Namely, if I'm issuing Ctrl-O in iPython running under iTerm2, nothing happens.
If I run stty discard undef before running iPython, issuing Ctrl-O in iPython leads to bell.
-
Honestly, what's wrong with this?
hist -n rerun lineN-lineM
-n shows the line numbers; rerun just runs those lines -- no <Enter> needed.
- If you're running all the lines, why not put them in a normal script and edit with VSCode. This makes it trivial to re-run all the lines with one keystroke. You have a debugger handy, and can re-run multiple times in the output pane with just one up-arrow.