我不确定我是否理解问题。您可以使用readline.clear_history和readline.add_history设置所需的可完成字符串,然后使用control-r搜索历史记录中的backword(就像在shell提示符下一样)。例如:
#!/usr/bin/env pythonimport readlinereadline.clear_history()readline.add_history('foo')readline.add_history('bar')while 1: print raw_input('> ')或者,您可以编写自己的完成程序版本并将适当的密钥绑定到该版本。如果匹配列表很大,此版本将使用缓存:
#!/usr/bin/env pythonimport readlinevalues = ['Paul Eden <paul@domain.com>','Eden Jones <ejones@domain.com>','Somebody Else <somebody@domain.com>']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 Nonereadline.set_completer(completer)readline.parse_and_bind('tab: menu-complete')while 1: a = raw_input('> ') print 'said:', a


