tags:

views:

45

answers:

3

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

+1  A: 

There is the way you can do it:

''.join(_ip.IP.shell.input_hist).split('\n')

or

''.join(_ip.IP.shell.input_hist_raw).split('\n')

to prevent magick expansion.

Alexander Artemenko
+1  A: 

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.scipy.org/doc/stable/html/interactive/reference.html#input-caching-system

PreludeAndFugue
+1  A: 

If you want to re-run a command in your history, try Ctrl-r and then your search string.

fmark