Howto Do Python Command-line Autocompletion But Not Only At The Beginning Of A String
Python, through it's readline bindings allows for great command-line autocompletion (as described in here). But, the completion only seems to work at the beginning of strings. If
Solution 1:
I'm not sure I understand the problem. You could use readline.clear_history and readline.add_history to set up the completable strings you want, then control-r to search backword in the history (just as if you were at a shell prompt). For example:
#!/usr/bin/env python
import readline
readline.clear_history()
readline.add_history('foo')
readline.add_history('bar')
while1:
print raw_input('> ')
Alternatively, you could write your own completer version and bind the appropriate key to it. This version uses caching in case your match list is huge:
#!/usr/bin/env pythonimport readline
values = ['Paul Eden <paul@domain.com>',
'Eden Jones <ejones@domain.com>',
'Somebody Else <somebody@domain.com>']
completions = {}
defcompleter(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:
returnNone
readline.set_completer(completer)
readline.parse_and_bind('tab: menu-complete')
while1:
a = raw_input('> ')
print'said:', a
Post a Comment for "Howto Do Python Command-line Autocompletion But Not Only At The Beginning Of A String"