Tab-Complete and a Persistent History in PythonNovember 17, 2006 at 9:15 pm | Posted in python | 4 Comments
Earlier today, in #django on freenode, the topic of tab-complete in the python shell happened to come up – at first, I thought it wasn’t possible in standard python, and only in other python interpreters.
Thankfully, a link was posted with info on the readline and rlcomplete modules – at first, I couldn’t get their example working to load on startup, so upon further investigation, not only did I get that quirk worked out, but I stumbled across another module that lets you hook on start/exit to save/load a history file, much like bash.
Anyway, without any further ado, here’s the short and sweet version to get this setup:
import atexit import os historyPath = os.path.expanduser("~/.pyhistory") try: import readline except ImportError: print "Module readline not available." else: import rlcompleter readline.parse_and_bind("tab: complete") if os.path.exists(historyPath): readline.read_history_file(historyPath) def save_history(historyPath=historyPath): try: import readline except ImportError: print "Module readline not available." else: readline.write_history_file(historyPath) atexit.register(save_history)
Drop that in a file such as .pythonrc in your home directory, then point python to that by adding the path to the PYTHONSTARTUP environment variable in .bashrc or .bash_profile
Sorry, the comment form is closed at this time.