Skip to content

Instantly share code, notes, and snippets.

@emsi
Forked from guyskk/.pythonrc.py
Created February 7, 2019 20:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emsi/17efe905c2f91407eb59bb6de4dda1ca to your computer and use it in GitHub Desktop.
Save emsi/17efe905c2f91407eb59bb6de4dda1ca to your computer and use it in GitHub Desktop.
Add command history and tab completion to python shell.
"""
Add command history and tab completion to python shell.
1. Save this file in ~/.pythonrc.py
2. Add the following line to your ~/.bashrc:
export PYTHONSTARTUP="$HOME/.pythonrc.py"
"""
def _enable_history_and_completer():
import atexit
import os.path
try:
import readline
except ImportError:
return
try:
import rlcompleter
except ImportError:
return
HISTORY_PATH = os.path.expanduser('~/.python_history')
def _save_history():
readline.write_history_file(HISTORY_PATH)
readline.set_completer(rlcompleter.Completer().complete)
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
if os.path.exists(HISTORY_PATH):
readline.read_history_file(HISTORY_PATH)
readline.set_history_length(10000)
atexit.register(_save_history)
_enable_history_and_completer()
del _enable_history_and_completer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment