python tab completion Mac OSX 10.7 (Lion)

前端 未结 2 1577
终归单人心
终归单人心 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
    
    0 讨论(0)
  • 2020-11-30 21:02

    Apple does not ship GNU readline with OS X. It does ship BSD libedit which includes a readline compatibility interface. The system Pythons shipped by Apple and the 64-bit/32-bit Pythons from python.org installers are built with libedit. The problem is that the commands supported by libedit are completely different from those of readline (see for example the discussion here). The traditional 32-bit-only python.org installers do use GNU readline as do some other 3rd-party distributors of Python for OS X, like MacPorts. Chances are that you were previously using such a Python and not a recent Apple one. You do have a few options, besides modifying Django: you can install the third-party replacement readline module; or you can use another Python that comes with GNU readline. However, you should not use the python.org 32-bit-only Pythons on 10.7 because, unfortunately, Xcode 4 on 10.7 no longer includes gcc-4.0 and the OS X 10.4u SDK which those Pythons need to build and install packages with C extension modules.

    Putting the following in the python startup file will enable tab completion for both the libedit interface and the typical readline module. For more information on the python startup file, see here

    import readline
    import rlcompleter
    if 'libedit' in readline.__doc__:
        readline.parse_and_bind("bind ^I rl_complete")
    else:
        readline.parse_and_bind("tab: complete")
    
    0 讨论(0)
提交回复
热议问题