Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

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

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:(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 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=/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
        readline.write_history_file(historyPath)
    
    if os.path.exists(historyPath):
        readline.read_history_file(historyPath)
    
    atexit.register(save_history)
    del os, atexit, readline, rlcompleter
    del save_history, historyPath

 
 
  Published under the terms of the Python License Design by Interspire