How to make a python, command-line program autocomplete arbitrary things NOT interpreter

Use Python’s readline bindings. For example,

import readline

def completer(text, state):
    options = [i for i in commands if i.startswith(text)]
    if state < len(options):
        return options[state]
    else:
        return None

readline.parse_and_bind("tab: complete")
readline.set_completer(completer)

The official module docs aren’t much more detailed, see the readline docs for more info.

Leave a Comment