Configuring the REPL's tab completion

Thomas Jollans tjol at tjol.eu
Mon Mar 11 06:37:08 EDT 2019


On 10/03/2019 15.20, Chris Angelico wrote:
> I have absolutely no idea how to do this or even where to go looking,
> so I'd appreciate a starting pointer :)
> 
> When you're in the Python REPL (just the basic core one, not IDLE or
> anything), you can tab-complete global and built-in names, attributes
> of known objects, etc. But quoted strings work kinda weirdly - they
> try to tab-complete a global or keyword:
> 
> Python 3.8.0a0 (heads/master:8b9c33ea9c, Nov 20 2018, 02:18:50)
> [GCC 6.3.0 20170516] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> "in
> in      input(  int(
>>>> "input("
> 'input('
> 
> I typed "in and hit tab twice, then typed p and hit tab, enter. It
> filled in the function name *input*, added an open parenthesis... and
> then closed the quote. Which doesn't make a lot of sense, but then,
> tab completing globals and keywords inside a text string doesn't make
> that much sense either.
> 
> What would be more useful would be tab-completing file names from the
> current directory.
> 
> open("Foo<tab>
> 
> to fill in the name of a file. Sure, not every quoted string is a file
> name (in fact, very few are), but I don't know of anything else that
> would feel natural. (Also, Pike's REPL behaves this way, so presumably
> it's of use to more people than me.)
> 
> Where would I start looking to try to make this happen? Doesn't
> necessarily have to be pushed upstream as a core Python feature; I'm
> guessing this can probably be done in sitecustomize.py. Anyone have
> tutorials on messing with tab completion? There's not a lot of info in
> the rlcompleter module docs.

I had a quick look at the code -

rlcompleter sets itself as the completer
<https://github.com/python/cpython/blob/master/Lib/rlcompleter.py#L200>
using readline.set_completer
<https://docs.python.org/3.6/library/readline.html#readline.set_completer>

I imagine you could write your own completer, and call set_completer
again after rlcompleter has been imported.

-- Thomas



More information about the Python-list mailing list