Show Menu

iPython History Cheat Sheet by

Describes iPython history

History flags summar­ized.

Flags for the history command

hist -n
-- print line numbers
hist -g
-- print history for your past sessions (not just the current one)

hist -f filena­
-- writes history lines to filena­
hist -l 10
-- will limit output to last 10 lines
hist -g timeit
-- will filter lines that contain the string "­tim­eit­"
hist -u -g timeit
-- same as filter, but will only print unique lines
hist -o -n
-- also print outputs

in iPython will give you similar info.

Previous iPython sessions

hist -n ~1/

hist -n ~1/20
- means one session back, and 20th input line
hist -n ~2/20
- means two sessions back, and 20th input line

hist -n 20
(i.e. without the ~ sign) defaults to the current ipython session.

Re-execute a history line

rerun <hi­story refere­nce>

rerun 20
will execute line 20 of the current session
rerun ~1/20
will execute line 20 of the previous session
rerun 88-90
will execute lines 88,89, 90 of history

Recalling input history (for inline editing)

recall <hi­story refere­nce>

recall 42
-- will recall line 42, and give you the prompt for editing
recall myfunc
-- will recall the most recent line containing myfunc, withe the same effect.

Hitting enter after the edit will execute.

Saving history to a file

save filena­ <hi­story refere­nce>

save mymodu­ 22-40 25
-- Saves lines 22-40, 25 to file mymodu­
save -a
-- will append

Grepping or filtering history lines

hist -g <reg exp>

hist -g func1
-- will list all history lines containing "­fun­c1"
hist -gn  func1
-- same thing. adds line numbers

Editing history in an editor

edit <hi­story refere­nce>

edit 24 28 47
- loads the lines in order, in your configured ipython editor

Output history editing via the "­_oh­" variable

Summary : Ipython allows you to edit both your past inputs, as well as past command outputs - so you if you want to avoid retyping this is handy. The built-in list variable "­_oh­" contains all your output history.

Syntax : "edit _oh[<l­ine-num of output histor­y>]­"

use "edit _oh[16­5]" to open output line 165 in your editor.

Example --
In [ 165 ] : def myfunc():
print "­hel­lo"
In [200] : edit 165 # opens line 165 in editor we made changes to that func w/o saving it to a file
out[200]: def myfunc()\n print "­hel­lo" \n
In [210]: edit _oh[200] # loads the func in editor
Use "edit -x" in case you are editing non-code stuff (to prevent ) execution when you leave the editor.

Edit a function that you defined inline

Syntax : edit <fu­ncn­ame>

Example - An inline function is defined intera­cti­vely, and then edited.

In [1] : def myfunc()
print "­hel­lo"

In [2] edit myfunc

Create a macro from history

Use the %macro command to create a macro from multiple history lines.

Example (lines 10 and 11 ) from history are as follows :
10: x=1
11: somefu­nc(x)

You can create a macro as follows :

In [20]: %macro my_macro 10-11

Now typing my_macro will execute those lines.


I came looking for how to list all history (including previous sessions).

Maybe add the answer?:

%history -g


Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter
          Jupyter Notebook Keyboard Shortcuts