How to indent blocks when readline completion is on?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Nov 13 08:30:37 EST 2011


I have set up readline completion as described here:

http://docs.python.org/library/rlcompleter.html

Now how do I indent blocks in the interactive interpreter? If I press the 
TAB key, the completer prompts me instead of indenting:

>>> readline.parse_and_bind("tab: complete")
>>> while True:
... 
Display all 182 possibilities? (y or n)
... 


Surely I'm not stuck with indenting by manually typing spaces?


I thought I could add a wrapper around the rlcompleter method, like this:

>>> import readline
>>> import rlcompleter
>>> readline.parse_and_bind("tab: complete")
>>> completer = readline.get_completer()
>>> def wrapped_completer(text, state):
...     if not text or text.isspace():
...         return "\t"
...     else:
...         return completer(text, state)
... 
>>> readline.set_completer(wrapped_completer)


Completion appears to work if I have something in the line to start with, 
e.g. if I type "whi" TAB "Tr" TAB I get "while True", but if I press TAB 
on an empty line (intending to get an actual tab character for 
indentation), it plays merry havoc with my session. All keyboard input 
appears to be dead, eventually I used Ctrl-Z to interrupt the process and 
killed it from the shell.


-- 
Steven



More information about the Python-list mailing list