11.3 Key Bindings

The key bindings and some other parameters of the Readline library can be customized by placing commands in an initialization file called "$HOME/.inputrc". Key bindings have the form

key-name: function-name

or

"string": function-name

and options can be set with

set option-name value

For example:

# I prefer vi-style editing:
set editing-mode vi
# Edit using a single line:
set horizontal-scroll-mode On
# Rebind some keys:
Meta-h: backward-kill-word
"\C-u": universal-argument
"\C-x\C-r": re-read-init-file

Note that the default binding for TAB in Python is to insert a TAB instead of Readline's default filename completion function. If you insist, you can override this by putting

TAB: complete

in your "$HOME/.inputrc". (Of course, this makes it hard to type indented continuation lines...)

Automatic completion of variable and module names is optionally available. To enable it in the interpreter's interactive mode, add the following to your "$HOME/.pythonrc" file:    

import rlcompleter, readline
readline.parse_and_bind('tab: complete')

This binds the TAB key to the completion function, so hitting the TAB key twice suggests completions; it looks at Python statement names, the current local variables, and the available module names. For dotted expressions such as string.a, it will evaluate the the expression up to the final "." and then suggest completions from the attributes of the resulting object. Note that this may execute application-defined code if an object with a __getattr__() method is part of the expression.

guido@python.org