Inheritance diagram for IPython.core.magics.history:
Implementation of magic functions related to History.
Bases: IPython.core.magic.Magics
Print docstring if incorrect arguments were passed
Make an entry in the options_table for fn, with value optstr
Format a string for latex inclusion.
Print input history (_i<n> variables), with most recent last.
%history [-o -p -t -n] [-f filename] [range | -g pattern | -l number]
By default, input history is printed without line numbers so it can be directly pasted into an editor. Use -n to show them.
By default, all input history from the current session is displayed. Ranges of history can be indicated using the syntax: 4 : Line 4, current session 4-6 : Lines 4-6, current session 243/1-5: Lines 1-5, session 243 ~2/7 : Line 7, session 2 before current ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
of 6 sessions ago.
Multiple ranges can be entered, separated by spaces
The same syntax is used by %macro, %save, %edit, %rerun
Options:
-n: print line numbers for each input. This feature is only available if numbered prompts are in use.
-o: also print outputs for each input.
- -p: print classic ‘>>>’ python prompts before each input. This is
- useful for making documentation, and in conjunction with -o, for producing doctest-ready output.
- -r: (default) print the ‘raw’ history, i.e. the actual commands you
- typed.
-t: print the ‘translated’ history, as IPython understands it. IPython filters your input and converts it all into valid Python source before executing it (things like magics or aliases are turned into function calls, for example). With this option, you’ll see the native history instead of the user-entered version: ‘%cd /’ will be seen as ‘get_ipython().magic(“%cd /”)’ instead of ‘%cd /’.
-g: treat the arg as a pattern to grep for in (full) history. This includes the saved history (almost all commands ever written). Use ‘%hist -g’ to show full saved history (may be very long).
-l: get the last n lines from all sessions. Specify n as a single arg, or the default is the last 10 lines.
- -f FILENAME: instead of printing the output to the screen, redirect
- it to the given file. The file is always overwritten, though when it can, IPython asks for confirmation first. In particular, running the command ‘history -f FILENAME’ from the IPython Notebook interface will replace FILENAME even if it already exists without confirmation.
Examples
In [6]: %hist -n 4-6
4:a = 12
5:print a**2
6:%hist -n 4-6
Print input history (_i<n> variables), with most recent last.
%history [-o -p -t -n] [-f filename] [range | -g pattern | -l number]
By default, input history is printed without line numbers so it can be directly pasted into an editor. Use -n to show them.
By default, all input history from the current session is displayed. Ranges of history can be indicated using the syntax: 4 : Line 4, current session 4-6 : Lines 4-6, current session 243/1-5: Lines 1-5, session 243 ~2/7 : Line 7, session 2 before current ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
of 6 sessions ago.
Multiple ranges can be entered, separated by spaces
The same syntax is used by %macro, %save, %edit, %rerun
Options:
-n: print line numbers for each input. This feature is only available if numbered prompts are in use.
-o: also print outputs for each input.
- -p: print classic ‘>>>’ python prompts before each input. This is
- useful for making documentation, and in conjunction with -o, for producing doctest-ready output.
- -r: (default) print the ‘raw’ history, i.e. the actual commands you
- typed.
-t: print the ‘translated’ history, as IPython understands it. IPython filters your input and converts it all into valid Python source before executing it (things like magics or aliases are turned into function calls, for example). With this option, you’ll see the native history instead of the user-entered version: ‘%cd /’ will be seen as ‘get_ipython().magic(“%cd /”)’ instead of ‘%cd /’.
-g: treat the arg as a pattern to grep for in (full) history. This includes the saved history (almost all commands ever written). Use ‘%hist -g’ to show full saved history (may be very long).
-l: get the last n lines from all sessions. Specify n as a single arg, or the default is the last 10 lines.
- -f FILENAME: instead of printing the output to the screen, redirect
- it to the given file. The file is always overwritten, though when it can, IPython asks for confirmation first. In particular, running the command ‘history -f FILENAME’ from the IPython Notebook interface will replace FILENAME even if it already exists without confirmation.
Examples
In [6]: %hist -n 4-6
4:a = 12
5:print a**2
6:%hist -n 4-6
Parse options passed to an argument string.
The interface is similar to that of getopt(), but it returns back a Struct with the options as keys and the stripped argument string still as a string.
arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.
-mode: default ‘string’. If given as ‘list’, the argument string is returned as a list (split on whitespace) instead of a string.
-list_all: put all option values in lists. Normally only options appearing more than once are put in a list.
-posix (True): whether to split the input line in POSIX mode or not, as per the conventions outlined in the shlex module from the standard library.
Repeat a command, or get command to input line for editing.
%recall and %rep are equivalent.
Place a string version of last computation result (stored in the special ‘_’ variable) to the next input prompt. Allows you to create elaborate command lines without using copy-paste:
In[1]: l = ["hei", "vaan"]
In[2]: "".join(l)
Out[2]: heivaan
In[3]: %rep
In[4]: heivaan_ <== cursor blinking
%recall 45
Place history line 45 on the next input prompt. Use %hist to find out the number.
%recall 1-4
Combine the specified lines into one cell, and place it on the next input prompt. See %history for the slice syntax.
%recall foo+bar
If foo+bar can be evaluated in the user namespace, the result is placed at the next input prompt. Otherwise, the history is searched for lines which contain that substring, and the most recent one is placed at the next input prompt.
Repeat a command, or get command to input line for editing.
%recall and %rep are equivalent.
Place a string version of last computation result (stored in the special ‘_’ variable) to the next input prompt. Allows you to create elaborate command lines without using copy-paste:
In[1]: l = ["hei", "vaan"]
In[2]: "".join(l)
Out[2]: heivaan
In[3]: %rep
In[4]: heivaan_ <== cursor blinking
%recall 45
Place history line 45 on the next input prompt. Use %hist to find out the number.
%recall 1-4
Combine the specified lines into one cell, and place it on the next input prompt. See %history for the slice syntax.
%recall foo+bar
If foo+bar can be evaluated in the user namespace, the result is placed at the next input prompt. Otherwise, the history is searched for lines which contain that substring, and the most recent one is placed at the next input prompt.
Re-run previous input
By default, you can specify ranges of input history to be repeated (as with %history). With no arguments, it will repeat the last line.
Options:
-l <n> : Repeat the last n lines of input, not including the current command.
-g foo : Repeat the most recent line which contains foo