python tab completion Mac OSX 10.7 (Lion)

前端 未结 2 1598
终归单人心
终归单人心 2020-11-30 20:29

Before upgrading to lion, I had tab complete working in a python shell via terminal. Following these instructions, it was possible to have tab complete working.

Sinc

2条回答
  •  爱一瞬间的悲伤
    2020-11-30 20:51

    As it uses libedit/editline, the syntax to enable autocompletion is a little bit different. You can first force emacs bindings (as it is with readline if I'm not wrong) by typing :

    readline.parse_and_bind("bind -e")

    Then you can add autocompletion linked to your TAB button (man editrc) :

    readline.parse_and_bind("bind '\t' rl_complete")

    And if you want to support indenting and has a history (found on internet), it should look like that (unless I made a mistake) :

    import readline,rlcompleter
    
    ### Indenting
    class TabCompleter(rlcompleter.Completer):
        """Completer that supports indenting"""
        def complete(self, text, state):
            if not text:
                return ('    ', None)[state]
            else:
                return rlcompleter.Completer.complete(self, text, state)
    
    readline.set_completer(TabCompleter().complete)
    
    ### Add autocompletion
    if 'libedit' in readline.__doc__:
        readline.parse_and_bind("bind -e")
        readline.parse_and_bind("bind '\t' rl_complete")
    else:
        readline.parse_and_bind("tab: complete")
    
    ### Add history
    import os
    histfile = os.path.join(os.environ["HOME"], ".pyhist")
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    import atexit
    atexit.register(readline.write_history_file, histfile)
    del histfile
    

提交回复
热议问题