Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Created December 30, 2010 20:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mgedmin/760273 to your computer and use it in GitHub Desktop.
Save mgedmin/760273 to your computer and use it in GitHub Desktop.
snippets for export PYTHONSTARTUP=~/.pythonrc
# 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
@mazunki
Copy link

mazunki commented May 31, 2022

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?

@mgedmin
Copy link
Author

mgedmin commented Jun 1, 2022

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.

@mazunki
Copy link

mazunki commented Jun 1, 2022

Thanks. I arrived here when looking at how to change the histfile, which was what I wanted.

I've tweaked it a bit :)

I am kind of curious: why do we import rlcompleter when, per documentation, importing readline already enables tab completion? Is it a version thing?

@mgedmin
Copy link
Author

mgedmin commented Sep 7, 2022

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment