-
-
Save emsi/17efe905c2f91407eb59bb6de4dda1ca to your computer and use it in GitHub Desktop.
Add command history and tab completion to python shell.
This file contains 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 | |
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