# Enable autocompletion and history. Originally from # http://igotgenes.blogspot.com/2009/01/tab-completion-and-history-in-python.html # but needed to tune it. import rlcompleter import atexit import os.path class IrlCompleter(rlcompleter.Completer): def __init__(self, tab=' '): self.tab = tab self.completer = rlcompleter.Completer() def complete(self, text, state): if not readline.get_line_buffer().strip(): readline.insert_text(self.tab) return None else: retval = self.completer.complete(text, state) return retval try: import readline except ImportError: print "Module readline not available, not enabling autocompletion." else: readline.parse_and_bind("tab: complete") readline.set_completer(IrlCompleter().complete) # Restore our command-line history, and save it when Python exits. history_path = os.path.expanduser('~/.pyhistory') if os.path.isfile(history_path): readline.read_history_file(history_path) atexit.register(lambda x=history_path: readline.write_history_file(x))