我不确定我是否理解这个问题.您可以使用readline.clear_history和readline.add_history来设置所需的可填写字符串,然后使用control-r搜索历史记录中的后向字符(就像您在shell提示符下一样).例如:
#!/usr/bin/env python
import readline
readline.clear_history()
readline.add_history('foo')
readline.add_history('bar')
while 1:
print raw_input('> ')
或者,您可以编写自己的完成版本并将相应的键绑定到它.此版本使用缓存,以防您的匹配列表很大:
#!/usr/bin/env python
import readline
values = ['Paul Eden ','Somebody Else ']
completions = {}
def completer(text,state):
try:
matches = completions[text]
except KeyError:
matches = [value for value in values
if text.upper() in value.upper()]
completions[text] = matches
try:
return matches[state]
except IndexError:
return None
readline.set_completer(completer)
readline.parse_and_bind('tab: menu-complete')
while 1:
a = raw_input('> ')
print 'said:',a