GNU Info

Info Node: (python2.1-tut.info)Key Bindings

(python2.1-tut.info)Key Bindings


Next: Commentary Prev: History Substitution Up: Interactive Input Editing and History Substitution
Enter node , (file) or (file)node

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

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

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:(1)

     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.

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 environments.  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, e.g. "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
     
     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)
     del os, atexit, readline, rlcompleter, save_history, historyPath

---------- Footnotes ----------

(1)  Python will execute the contents of a file identified by the
`PYTHONSTARTUP' environment variable when you start an interactive
interpreter.


automatically generated by info2www version 1.2.2.9