Tab completion in Python3.4

Steven D'Aprano steve at pearwood.info
Mon Sep 30 01:38:03 EDT 2013


By default, Python 3.4 will ship with tab completion turned on. When you 
hit the tab key, Python will try to complete the current function, 
method, variable or other name, if necessary displaying the alternatives 
is there are more than one:


py> d = {}
py> d.pop
d.pop(      d.popitem(  


This is a great feature, and very welcome to become on by default. Many 
thanks to Antoine Pitrou for finally making this happen! I've been using 
tab completion using a custom startup file for a few years, and of course 
anyone familiar with the common Linux shells will also be familiar with 
it.

But there is one sting in the tail: the current 3.4 implementation makes 
it impossible to use the tab key to indent code. Pressing TAB at the 
start of the line tries to complete on *everything*, instead of indenting:


py> for x in range(3):
... 
Display all 195 possibilities? (y or n)


This is a problem: it means you either have to indent using multiple 
spaces, which is a pain, or a single space, which is ugly and hard to 
read:

py> for x in range(3):
...  print(x)
...
0
1
2


I don't consider either of these solutions to be satisfactory. If you 
agree, I urge you to try it out for yourself, and then leave a comment on 
the bug tracker asking for tab completion to still insert tabs at the 
beginning of the line:

http://bugs.python.org/issue5845



For anyone wanting to see my version of tab completion and history:

https://code.google.com/p/my-startup-file/source/browse/completer.py
https://code.google.com/p/my-startup-file/source/browse/history.py


Usage is simple: in my startup file, or just do it manually:

try:
    import history
except ImportError:
    print('*** warning: command line history not available ***')
else:
    history = history.History()

try:
    import completer
except ImportError:
    print('*** warning: command line completion not available ***')
else:
    completer = completer.Completer(
                    bindings=(r'"\C-xo": overwrite-mode',
                              r'"\C-xd": dump-functions',
                              )
                            )



-- 
Steven



More information about the Python-list mailing list