Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:51:15+00:00 2026-05-26T08:51:15+00:00

I’d like to write code that does autocompletion in the Linux terminal. The code

  • 0

I’d like to write code that does autocompletion in the Linux terminal. The code should work as follows.

It has a list of strings (e.g. "hello, "hi", "how are you", "goodbye", "great", …).

In the terminal, the user will start typing and when there is some match possibility, he gets the hint for possible strings, from which he can choose (similarly as in vim editor or google incremental search).

e.g. he starts typing "h", and he gets the hint

h"ello"

_ "i"

_"ow are you"

And better yet would be if it would complete words not only from the beginning, but from an arbitrary part of the string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:51:15+00:00Added an answer on May 26, 2026 at 8:51 am

    (I’m aware this isn’t exactly what you’re asking for, but) If you’re happy with the auto-completion/suggestions appearing on TAB (as used in many shells), then you can quickly get up and running using the readline module.

    Here’s a quick example based on Doug Hellmann’s PyMOTW writeup on readline.

    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
    

    This results in the following behaviour (<TAB> representing a the tab key being pressed):

    Input: <TAB><TAB>
    goodbye      great        hello        hi           how are you
    
    Input: h<TAB><TAB>
    hello        hi           how are you
    
    Input: ho<TAB>ow are you
    

    In the last line (HOTAB entered), there is only one possible match and the whole sentence “how are you” is auto completed.

    Check out the linked articles for more information on readline.


    “And better yet would be if it would complete words not only from the beginning … completion from arbitrary part of the string.”

    This can be achieved by simply modifying the match criteria in the completer function, ie. from:

    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    

    to something like:

    self.matches = [s for s in self.options 
                       if text in s]
    

    This will give you the following behaviour:

    Input: <TAB><TAB>
    goodbye      great        hello        hi           how are you
    
    Input: o<TAB><TAB>
    goodbye      hello        how are you
    

    Updates: using the history buffer (as mentioned in comments)

    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 Ctrl+R to perform a reverse-search.

    To try this out, make the following changes:

    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
    

    When you run the script, try typing Ctrl+r followed by a. That will return the first match that contains “a”. Enter Ctrl+r again for the next match. To select an entry, press ENTER.

    Also try using the UP/DOWN keys to scroll through the keywords.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.