Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@guyskk
Last active May 25, 2020 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save guyskk/6f3522e3d17135b470bf3d982c80cc01 to your computer and use it in GitHub Desktop.
Save guyskk/6f3522e3d17135b470bf3d982c80cc01 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
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