Tab completion

Miles semanticist at gmail.com
Sun Apr 5 22:48:52 EDT 2009


On Thu, Apr 2, 2009 at 10:59 AM, Steven D'Aprano wrote:
> Does anyone use the tab-completion recipe in the docs?
>
> http://docs.python.org/library/rlcompleter.html#module-rlcompleter
>
> suggests using this to enable tab-completion:
>
> try:
>    import readline
> except ImportError:
>    print "Module readline not available."
> else:
>    import rlcompleter
>    readline.parse_and_bind("tab: complete")
>
> which is all very nice, but it makes it rather difficult to indent code
> blocks:
>
>>>> def func(x):
> ...
> Display all 174 possibilities? (y or n)
>
>
> I like tab-completion, but I'd rather not be reduced to typing spaces for
> indents in the interpreter. What do other people do?

Personally, I just use single spaces for indentation when typing in
the interpreter.  But how would something like this work for you?

import readline
import rlcompleter

class TabCompleter(rlcompleter.Completer):
    def complete(self, text, state):
        if text == '' or text.endswith('\t'):
            if state == 0:
                return text + '\t'
            else:
                return None
        return rlcompleter.Completer.complete(self, text, state)

readline.set_completer(TabCompleter().complete)

-Miles



More information about the Python-list mailing list