How to search python interpreter history

To search the python interpreter history we can use the following code as it is with the string search_string replaced by string to be searched.

>>> import readline
>>> for i in range(readline.get_current_history_length()):
…       x=readline.get_history_item(i+1)
…       if “search_string” in x:
…          print(x)

Note: The indentation of the code is crucial to its function.

Leave a comment