User Tools

Site Tools


python:colorization_and_tab_completion_for_the_python_shell

Make a .pythonrc file:

# Python startup environment -- color prompt and tab completion
# 2012-08-14  --  Tyler Bletsch
 
import sys,os,re
import itertools
 
if sys.stdout.isatty():
	try:
		import readline
	except ImportError:
		print "Module readline not available."
	else:
		import rlcompleter
		readline.parse_and_bind("tab: complete")
 
	# ANSI coloring with readline escape codes
	def c(s=''): return '\001\033[%sm\002' % s  
 
	if os.environ.get('TERM') in ('xterm', 'vt100'):
		sys.ps1 = c('0;36;96;1') + '>>> ' + c('0;36')
		sys.ps2 = c('0;36;96;1') + '... ' + c('0;36')
 
		def displayhook(v):
			sys.stdout.write('\033[0;32;92m')
			sys.__displayhook__(v)
			sys.stdout.write('\033[m')
		sys.displayhook = displayhook
		del displayhook
 
		def excepthook(type, value, traceback):
			sys.stdout.write('\033[31;91m')
			sys.__excepthook__(type, value, traceback)
			sys.stdout.write('\033[m')
		sys.excepthook = excepthook
		del excepthook
 
	sys.stderr.write(c('36;1') + "Custom extensions enabled." + c() + "\n")
 
	del c

Set environment variable to enable it:

PYTHONSTARTUP=$HOME/.pythonrc

That's all.

python/colorization_and_tab_completion_for_the_python_shell.txt · Last modified: 2012/08/14 21:20 by tkbletsc