小编典典
我不确定我是否理解问题。您可以使用readline.clear_history和readline.add_history设置所需的可完成字符串,然后使用control-r搜索历史记录中的backword(就像在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 ',
'Eden Jones ',
'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
2020-06-07