06 May 2013

Python shell tab completion

A comment thread on HN this morning taught me a useful trick for the vanilla Python shell - tab completion.

In ~/.pythonstartup.py:

AP_AUTOCOMPLETE=False
import sys
try:
    import readline
except ImportError:
    print "Module readline unavailable"
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")
    if sys.platform == 'darwin':
        readline.parse_and_bind("bind ^I rl_complete")
    AP_AUTOCOMPLETE=True

And then in your ~/.bashrc/~/.zshrc:

export PYTHONSTARTUP="$HOME/.pythonstartup.py"

Thanks to est, gsa, niggler & alexpopescu for commenting here.

:x


Tagged in tekkers