user3115352
user3115352

Reputation: 71

Readline autocomplete and whitespace

I am using readline in python for autocompletion

I am trying to have it so if i have:

names = ['ben dodds', 'james asda', 'izzy smidt']

When I have

> Insert name: 
> be TAB
ben dodds
> ben TAB
ben dodds, james asda, izzy smidt

The last one is wrong as it should only show ben dodds

This is my code so far:

def completer(text, state):
    options = [x for x in names if x.startswith(text)]
    try:
        return options[state]
    except IndexError:
        return None



readline.set_completer(completer)
if 'libedit' in readline.__doc__:
    readline.parse_and_bind("bind ^I rl_complete")
else:
    readline.parse_and_bind("tab: complete")

name = raw_input('Enter name\n')
print name

So far I have gathered it is something to do with readline.set_completer_delims() but I cant figure out how to get it to work, any help would be appreciated

I have already tried

    readline.set_completer_delims('')

It didnt work

Upvotes: 2

Views: 2062

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114946

Try this:

readline.set_completer_delims('')

I added that line to your script as follows:

import readline


names = ['ben dodds', 'james asda', 'izzy smidt']

def completer(text, state):
    options = [x for x in names if x.startswith(text)]
    try:
        return options[state]
    except IndexError:
        return None


readline.set_completer(completer)
readline.set_completer_delims('')

if 'libedit' in readline.__doc__:
    readline.parse_and_bind("bind ^I rl_complete")
else:
    readline.parse_and_bind("tab: complete")

name = raw_input('Enter name\n')
print name

That fixed the issue for me (using python 2.7.3 or 2.7.6 on 64 bit Ubuntu 12.04).

Upvotes: 3

Related Questions