Skip to content

Instantly share code, notes, and snippets.

@artem-bez
Last active January 2, 2016 12:19
Show Gist options
  • Save artem-bez/8302155 to your computer and use it in GitHub Desktop.
Save artem-bez/8302155 to your computer and use it in GitHub Desktop.
Add auto-completion and a stored history file of commands to vanilla Python interactive interpreter.
# Add auto-completion and a stored history file of commands to vanilla Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in $HOME/.pystartup, and set an environment variable to point
# to it: "export PYTHONSTARTUP=$HOME/.pystartup" in bash.
def init():
init_tab_completion()
init_history()
def init_tab_completion():
import rlcompleter
readline.parse_and_bind("tab: complete")
def init_history():
import atexit
import os
import readline
import rlcompleter
import sys
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
try:
import readline
except ImportError:
print ("Module readline is not available. "
"History and completion will be unavailable.")
else:
init()
# cleanup global namespace
del readline
finally:
del init, init_history, init_tab_completion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment