-
-
Save mgedmin/760273 to your computer and use it in GitHub Desktop.
# This is the actual file that combines all of the other snippets | |
# I've been using it for a while, so it's officially Bug Free (TM) | |
# Python startup script. vim: set ft=python : | |
# from http://www.norvig.com/python-iaq.html | |
# also see Tarek Ziade's _Expert_Pythom_Programming_ page 19 | |
import os, sys | |
# Coloured prompt | |
if os.getenv('TERM') in ('xterm', 'vt100', 'rxvt', 'Eterm', 'putty'): | |
try: | |
import readline | |
except ImportError: | |
sys.ps1 = '\033[0;32m>>> \033[0m' | |
sys.ps2 = '\033[0;32m... \033[0m' | |
else: | |
sys.ps1 = '\001\033[0;32m\002>>> \001\033[0m\002' | |
sys.ps2 = '\001\033[0;32m\002... \001\033[0m\002' | |
# Completion! | |
try: | |
import readline | |
except ImportError: | |
print("Module readline not available.") | |
else: | |
# persistent history | |
histfile = os.path.expanduser('~/.pythonhistory') | |
try: | |
readline.read_history_file(histfile) | |
except IOError: | |
pass | |
import atexit | |
atexit.register(readline.write_history_file, histfile) | |
del histfile, atexit | |
# tab completion | |
try: | |
sys.path.append(os.path.join(os.getenv('HOME'), 'src', 'rlcompleter2')) | |
import rlcompleter2 | |
rlcompleter2.setup() | |
del rlcompleter2 | |
except ImportError: | |
import rlcompleter | |
readline.parse_and_bind("tab: complete") | |
del rlcompleter | |
del readline | |
del sys | |
del os |
# Coloured prompt | |
import os, sys | |
if os.getenv('TERM') in ('xterm', 'vt100', 'rxvt', 'Eterm', 'putty'): | |
try: | |
import readline | |
except ImportError: | |
sys.ps1 = '\033[0;32m>>> \033[0m' | |
sys.ps2 = '\033[0;32m... \033[0m' | |
else: | |
sys.ps1 = '\001\033[0;32m\002>>> \001\033[0m\002' | |
sys.ps2 = '\001\033[0;32m\002... \001\033[0m\002' | |
del readline | |
del os, sys |
# Persistent history | |
try: | |
import readline | |
except ImportError: | |
print("Module readline not available.") | |
else: | |
import os | |
histfile = os.path.expanduser('~/.pythonhistory') | |
try: | |
readline.read_history_file(histfile) | |
except IOError: | |
pass | |
import atexit | |
atexit.register(readline.write_history_file, histfile) | |
del histfile, atexit, readline, os |
# Tab-completion | |
try: | |
import readline | |
except ImportError: | |
print("Module readline not available.") | |
else: | |
import os, sys | |
try: | |
# look for rlcompleter2 in ~/src, it's a bit more advanced | |
sys.path.append(os.path.expanduser('~/src/rlcompleter2')) | |
import rlcompleter2 | |
rlcompleter2.setup() | |
del rlcompleter2 | |
except ImportError: | |
# fall back to rlcompleter from the standard library | |
import rlcompleter | |
readline.parse_and_bind("tab: complete") | |
del rlcompleter | |
del readline, os, sys |
I'm currently making a script/package to enforce XDG standards. Is it alright if I yank'n'paste your script (removing the coloured prompt) into it?
I'm not sure what my Python REPL tweaks have to do with XDG standards, but feel free. It's not really my code anyway, I've seen bits and pieces on the Internet and assembled them.
Thanks. I arrived here when looking at how to change the histfile, which was what I wanted.
I am kind of curious: why do we import rlcompleter
when, per documentation, importing readline
already enables tab completion? Is it a version thing?
readline
enables command-line editing and provides a hook for custom tab-completion. rlcompleter
implements a readline tab-completion hook that can understand and complete Python code. rlcompleter2 was a slightly more advanced version, but seems unmaintained these days (last release in 2010).
Moved to https://github.com/mgedmin/dotfiles/blob/master/python
see also https://github.com/mgedmin/dotfiles/blob/master/bashrc.python