I ran across this page of tips for the interactive Python interpreter the other day. It's part of the official Python documentation, but kinda buried in searches for "pythonrc" and "python interpreter", maybe because the example uses ".pystartup" for the name of the configuration file (a practice I'll follow as well, seeing as pythonrc is used for other things). I'm rambling on about it here so I don't lose or forget about it down the road. I realize ipython does everything that's described therein, but, frankly, it's too flashy for me, and I like being able to copy and paste into my docstrings for doctests.
I've been using the 'tab: complete' mapping for a few days, and I
like it. It's kind of a bummer to have to hit the spacebar four times
for each indent (and I agree with the commentary at the bottom that
the interpreter should suggest proper indentation), but it's not a big
deal for me. The default, mapping autocomplete to the Esc key, just
feels weird. I tried '`: complete' (mapping the backtick to
autocomplete), and that works, but tab-completion is so ingrained in
me that I don't really want to train myself on another key (though I
have adapted to Ctrl-P in vim).
Ooh, and a saved history between sessions is really the only other thing I like about ipython. Glad to see it can be done in the boring old, plain old interactive interpreter session.
My .pystartup, then, looks much like the example in the Python docs:
# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it: "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.
import atexit
import os
import readline
import rlcompleter
import sys
# change autocomplete to tab
readline.parse_and_bind("tab: complete")
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
# anything not deleted (sys and os) will remain in the interpreter session
del atexit, readline, rlcompleter, save_history, historyPath
breaksalot.org