How do you see the entire command history in interactive Python?
Code for printing the entire history:
Python 3
One-liner (quick copy and paste):
import readline; print('\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())]))
(Or longer version...)
import readline
for i in range(readline.get_current_history_length()):
print (readline.get_history_item(i + 1))
Python 2
One-liner (quick copy and paste):
import readline; print '\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())])
(Or longer version...)
import readline
for i in range(readline.get_current_history_length()):
print readline.get_history_item(i + 1)
Note: get_history_item()
is indexed from 1 to n.
Use readline.get_current_history_length()
to get the length, and readline.get_history_item()
to view each.
With python 3 interpreter the history is written to~/.python_history
If you want to write the history to a file:
import readline
readline.write_history_file('python_history.txt')
The help function gives:
Help on built-in function write_history_file in module readline:
write_history_file(...)
write_history_file([filename]) -> None
Save a readline history file.
The default filename is ~/.history.