[IPython-user] Custom command completers implemented in SVN

Ville M. Vainio vivainio at gmail.com
Mon Oct 30 14:01:38 CST 2006

I have a preliminary (but functional, and probably final)
implementation of custom completer hooks implemented in SVN. Now it's
quite easy to add your own completers.
Here's an example (Extensions/ipy_linux_package_managers.py):


""" Tab completion support for a couple of linux package managers

This is also an example of how to write custom completer plugins
or hooks.

Practical use:

[ipython]|1> import ipy_linux_package_managers
[ipython]|2> apt-get u<<< press tab here >>>
update  upgrade
[ipython]|2> apt-get up

import IPython.ipapi

ip = IPython.ipapi.get()

def apt_completers(self, event):
    """ This should return a list of strings with possible completions.

    Note that all the included strings that don't start with event.symbol
    are removed, in order to not confuse readline.

    # print event # dbg

    # commands are only suggested for the 'command' part of package manager
    # invocation

    cmd = (event.line + "<placeholder>").rsplit(None,1)[0]
    # print cmd
    if cmd.endswith('apt-get') or cmd.endswith('yum'):
        return ['update', 'upgrade', 'install', 'remove']

    # later on, add dpkg -l / whatever to get list of possible
    # packages, add switches etc. for the rest of command line
    # filling

    raise IPython.ipapi.TryNext

# re_key specifies the regexp that triggers the specified completer

ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get')

ip.set_hook('complete_command', apt_completers, re_key = '.*yum')

Ville M. Vainio - vivainio.googlepages.com
blog=360.yahoo.com/villevainio - g[mail | talk]='vivainio'

More information about the IPython-user mailing list