# Clever trick to add history support and tab completion to cPython. # This was stolen from a fellow ubuntero's blog and extended to add # more error checking along with the ability to be ran system-wide. # # Save as /etc/pythonrc and put 'export PYTHONSTARTUP=/etc/pythonrc' in # /etc/bash.bashrc. This makes the functionality available system-wide. # # Cleaned up 7/11/2009 by Jeff Schroeder # Released under the public domain for all to share and share alike. try: import readline except ImportError: pass else: import os import atexit import rlcompleter class irlcompleter(rlcompleter.Completer): def complete(self, text, state): if text == "": readline.insert_text('\t') return None else: return rlcompleter.Completer.complete(self,text,state) # You could change this line to bind another key instead tab. readline.parse_and_bind("tab: complete") readline.set_completer(irlcompleter().complete) # Restore our command-line history, and save it when Python exits. historyPath = os.path.expanduser("~/.pyhistory") # Create a blank history file if it doesn't exist already if not os.path.exists(historyPath) and not os.path.isdir(historyPath): try: open(historyPath, 'w').close() # Gracefully ignore things if historyPath is not writable except IOError: pass # Read the history file in for autocompletion and save it on exit if os.access(historyPath, os.R_OK): readline.read_history_file(historyPath) if os.access(historyPath, os.W_OK): atexit.register(lambda x=historyPath: readline.write_history_file(x))