bgbg bgbg - 11 months ago 89
Python Question

Search inside ipython history


command outputs recent commands entered by the user. Is it possible to search within these commands? Something like this:

[c for c in %history if c.startswith('plot')]

EDIT I am not looking for a way to rerun a command, but to locate it in the history list. Of course, sometimes I will want to rerun a command after locating it, either verbatim or with modifications.

EDIT searching with
and then typing
gives the most recent command that starts with "plot". It won't list all the commands that start with it. Neither can you search within the middle or the end of the commands


Expanding PreludeAndFugue's solution here what I was looking for:

[l for l in _ih if l.startswith('plot')]

here, the
condition can be substituted by a regex

Answer Source

Similar to the first answer you can do the following:


However, when iterating through the command history items you can do the following. Thus you can create your list comprehension from this.

for item in _ih:
    print item

This is documented in the following section of the documentation: