Skip to content

Instantly share code, notes, and snippets.

@guyskk
Last active May 25, 2020
Embed
What would you like to do?
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
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