Last active
May 25, 2020 11:15
-
-
Save guyskk/6f3522e3d17135b470bf3d982c80cc01 to your computer and use it in GitHub Desktop.
Add command history and tab completion to python shell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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