Saturday, May 30, 2009

My .pythonrc.py


#!/usr/bin/python

import sys
import os
import atexit

# color prompt
sys.ps1 = '\001\033[1;36m\002>>> \001\033[0m\002'

# tab completion
# from http://www.doughellmann.com/PyMOTW/rlcompleter/index.html
try:
import readline
except ImportError:
# Silently ignore missing readline module
pass
else:
import rlcompleter
readline.parse_and_bind("tab: complete")

# history
# from http://dotfiles.org/~remote/.pythonrc.py
histfile = os.path.join(os.environ["HOME"], ".python_history")
try:
readline.read_history_file(histfile)
except IOError:
pass

atexit.register(readline.write_history_file, histfile)
del os, histfile


If running 'python' on the command line does not run the file, you can always alias python to 'python -i ~/.pythonrc.py'

4 comments:

Adam Rosenfield said...

On Mac OS X, the colorized prompt apparently screws up readline. If you type in a long line, the terminal wraps the line several columns before the end. Worse, when you do so, there's a carriage return but no newline, so you start overwriting the beginning of the line on the terminal (though it still remembers exactly what you typed in, you just can't read it).

Danny Colligan said...

For serious? I thought the \001 and \002 delineated the "don't count this as actual characters" section for readline. It works fine on Ubuntu. There's another way to escape characters in bash (and I think python approximately follows the bash standards for setting ps1) involving \033 (the octal escape character) if I remember correctly... anyways, here's more than you'll ever need on bash prompt customization: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/

Danny Colligan said...

Specifically, this section: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/nonprintingchars.html

Adam Rosenfield said...

Python doesn't seem to interpret the '\[' and '\]' sequences. Upon further investigation, the problem only occurs with certain versions of Python on OS X, specifically the Python 2.5.2 that came pre-installed, and the Python 2.6.1 from MacPorts (currently my default). Python 2.3.5 (pre-installed), 2.5.4 (MacPorts), and 3.0 (MacPorts) handle line wrapping properly.