In bash I can prevent a command from being saved to bash history by putting a space in front of it. I cannot use this method in ipython. How would I do the equivalent in ipython?
Obsolescence note: this was written for
IPython 4.0.1. As of v5, IPython no longer uses
No stock way, can be added in or worked around.
There are two kinds of history in IPython:
readline history. It is available with special keys/key combinations (up/down, Ctrl-R etc). Only stores lines entered (with
<Enter>) on the console (or, in
pyreadline, pasted from the clipboard, too). IPython relies on a local implementation of the Python
readline API which doesn't have a "do not add" function and normally adds every line to history.
IPython has a facility to alleviate this,
IPython.core.interactiveshell.ReadlineNoRecord, a context manager that makes a snapshot of history then restores it. As of
ipython 4.0.1, it's only used by
%run magic to avoid adding script interactive input to history.
IPython history. It's saved in the DB and automatic variables. It contains complete inputs (
readline grabs each
<Enter>'ed line separately for multiline ones) and outputs. Saving is implemented in
HistoryManager.store_inputs() (for inputs) and
HistoryManager.store_output() (for outputs) which is called from
InteractiveShell.run_cell and is governed by its
store_history argument. The latter is in turn called from
There are two ways to solve the problem for either layer:
prevent adding the input in the first place. This cannot be done with a magic prefixed to a command, only with one that is run as a separate command and toggles a flag. That's because, as you've seen, the current input has already been stored by the time a magic command gets control.
pyreadline, adding is done with
pyreadline.modes.basemode.BaseMode.add_history(). The mode object is accessible as
add_historywith wrappers checking flags in corresponding objects and a custom magic command that sets/toggles them should do the trick.
evidence input/output from history immediately after execution. This can be done with a prefix magic.
HistoryManagerdoesn't have any facilities to remove entries (neither from DB nor from variables). Alas, hacking the DB by hand/replacing stock
HistoryManageris necessary. Also note that the class has an optional cache of
HistoryManager.db_cache_size(disabled by default).
remove_history_item(index)is in the API. You need to know the number of lines in the input.
Alternatively, if you only require this to enter passwords, consider other ways that don't echo the password on screen (thus not making it a part of console history):