Stop tab-completing empty lines!

Carsten Haese carsten at uniqsys.com
Thu Jan 17 15:48:23 CET 2008


On Thu, 2008-01-17 at 00:58 -0800, Casey Rodarmor wrote:
> Hi everybody,
> 
> I have the following in my python startup file:
> 
> import readline, rlcompleter
> readline.parse_and_bind("tab: complete")
> 
> This lets me tab complete identifiers in the interpreter beautifully,
> but there's a tiny problem... If I'm sitting at the beginning of a
> blank line and just want a tab, it tries to tab complete, which kind
> of a pain. 
> 
> <BLINK>-=SIMULATED PYTHON PROMPT=-</BLINK>
> >>> def mega_awesome_function(cool_stuff, **some_sweet_kwargs):
> ... X
> 
> (The X is where I press tab and get super annoyed)

Patching rlcompleter.py in the python library thusly seems to do the
trick:

"""
--- rlcompleter.py.bak  2008-01-17 09:35:06.000000000 -0500
+++ rlcompleter.py      2008-01-17 09:35:08.000000000 -0500
@@ -99,6 +99,7 @@
         defined in self.namespace that match.
 
         """
+        if text=="": return ['\t']
         import keyword
         matches = []
         n = len(text)
"""

Note that this prevents tab-completion not only at the beginning of the
line but also at the beginning of a token, i.e. after a parenthesis or a
comma etc. I don't know if it's possible to have the completer
distinguish between the beginning of a token in the middle of the line
and the beginning of the line, though.

Hope this helps,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list