Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(I'm aware this isn't exactly what you're asking for, but) If you're happy with the auto-completion/suggestions appearing on <kbd>TAB</kbd> (as used in many shells), then you can quickly get up and running using the <a href="http://docs.python.org/library/readline.html" rel="noreferrer">readline</a> module.</p> <p>Here's a quick example based on <a href="https://doughellmann.com/blog/2008/11/30/pymotw-readline/" rel="noreferrer">Doug Hellmann's PyMOTW writeup on readline</a>.</p> <pre><code>import readline class MyCompleter(object): # Custom completer def __init__(self, options): self.options = sorted(options) def complete(self, text, state): if state == 0: # on first trigger, build possible matches if text: # cache matches (entries that start with entered text) self.matches = [s for s in self.options if s and s.startswith(text)] else: # no text entered, all matches possible self.matches = self.options[:] # return match indexed by state try: return self.matches[state] except IndexError: return None completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"]) readline.set_completer(completer.complete) readline.parse_and_bind('tab: complete') input = raw_input("Input: ") print "You entered", input </code></pre> <p>This results in the following behaviour (<code>&lt;TAB&gt;</code> representing a the tab key being pressed):</p> <pre><code>Input: &lt;TAB&gt;&lt;TAB&gt; goodbye great hello hi how are you Input: h&lt;TAB&gt;&lt;TAB&gt; hello hi how are you Input: ho&lt;TAB&gt;ow are you </code></pre> <p>In the last line (<kbd>H</kbd><kbd>O</kbd><kbd>TAB</kbd> entered), there is only one possible match and the whole sentence "how are you" is auto completed.</p> <p>Check out the linked articles for more information on <code>readline</code>.</p> <hr> <blockquote> <p>"And better yet would be if it would complete words not only from the beginning ... completion from arbitrary part of the string."</p> </blockquote> <p>This can be achieved by simply modifying the match criteria in the completer function, ie. from:</p> <pre><code>self.matches = [s for s in self.options if s and s.startswith(text)] </code></pre> <p>to something like:</p> <pre><code>self.matches = [s for s in self.options if text in s] </code></pre> <p>This will give you the following behaviour:</p> <pre><code>Input: &lt;TAB&gt;&lt;TAB&gt; goodbye great hello hi how are you Input: o&lt;TAB&gt;&lt;TAB&gt; goodbye hello how are you </code></pre> <hr> <h3>Updates: using the history buffer (as mentioned in comments)</h3> <p>A simple way to create a pseudo-menu for scrolling/searching is to load the keywords into the history buffer. You will then be able to scroll through the entries using the up/down arrow keys as well as use <kbd>Ctrl</kbd>+<kbd>R</kbd> to perform a reverse-search.</p> <p>To try this out, make the following changes:</p> <pre><code>keywords = ["hello", "hi", "how are you", "goodbye", "great"] completer = MyCompleter(keywords) readline.set_completer(completer.complete) readline.parse_and_bind('tab: complete') for kw in keywords: readline.add_history(kw) input = raw_input("Input: ") print "You entered", input </code></pre> <p>When you run the script, try typing <kbd>Ctrl</kbd>+<kbd>r</kbd> followed by <kbd>a</kbd>. That will return the first match that contains "a". Enter <kbd>Ctrl</kbd>+<kbd>r</kbd> again for the next match. To select an entry, press <kbd>ENTER</kbd>.</p> <p>Also try using the UP/DOWN keys to scroll through the keywords.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload