bgbg bgbg - 3 months ago 19
Python Question

Search inside ipython history

ipython's

%his
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
ctr-r
and then typing
plot
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

Solution



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

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


here, the
if
condition can be substituted by a regex

Answer

Similar to the first answer you can do the following:

''.join(_ih).split('\n')

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: http://ipython.org/ipython-doc/dev/interactive/reference.html#input-caching-system

Comments