- publishing free software manuals
An Introduction to Python
by Guido van Rossum and Fred L. Drake, Jr.
Paperback (6"x9"), 124 pages
ISBN 0954161769
RRP £12.95 ($19.95)

Sales of this book support the Python Software Foundation! Get a printed copy>>>

12.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 ‘~/.inputrc’. Key bindings have the form

    key-name: function-name


    "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 character instead of Readline's default filename completion function. If you insist, you can override this by putting

    Tab: complete

in your ‘~/.inputrc’. (Of course, this makes it harder to type indented continuation lines if you're accustomed to using Tab for that purpose.)

Automatic completion of variable and module names is optionally available. To enable it in the interpreter's interactive mode, add the following to your startup file:(5)

    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 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.

A more capable startup file might look like this example. Note that this deletes the names it creates once they are no longer needed; this is done since the startup file is executed in the same namespace as the interactive commands, and removing the names avoids creating side effects in the interactive environment. You may find it convenient to keep some of the imported modules, such as ‘os’, which turn out to be needed in most sessions with the interpreter.

    # 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=/home/guido/.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
    historyPath = os.path.expanduser("~/.pyhistory")
    def save_history(historyPath=historyPath):
        import readline
    if os.path.exists(historyPath):
    del os, atexit, readline, rlcompleter
    del save_history, historyPath
ISBN 0954161769An Introduction to PythonSee the print edition