views:

1250

answers:

4

Several months ago, I wrote a blog post detailing how to achieve tab-completion in the standard Python interactive interpreter--a feature I once thought only available in IPython. I've found it tremendously handy given that I sometimes have to switch to the standard interpreter due to IPython unicode issues.

Recently I've done some work in OS X. To my discontent, the script doesn't seem to work for OS X's Terminal application. I'm hoping some of you with experience in OS X might be able to help me trouble-shoot it so it can work in Terminal, as well.

I am reproducing the code below

import atexit
import os.path

try:
    import readline
except ImportError:
    pass
else:
    import rlcompleter

    class IrlCompleter(rlcompleter.Completer):
        """
        This class enables a "tab" insertion if there's no text for
        completion.

        The default "tab" is four spaces. You can initialize with '\t' as
        the tab if you wish to use a genuine tab.

        """

        def __init__(self, tab='    '):
            self.tab = tab
            rlcompleter.Completer.__init__(self)


        def complete(self, text, state):
            if text == '':
                readline.insert_text(self.tab)
                return None
            else:
                return rlcompleter.Completer.complete(self,text,state)


    #you could change this line to bind another key instead tab.
    readline.parse_and_bind('tab: complete')
    readline.set_completer(IrlCompleter('\t').complete)


# Restore our command-line history, and save it when Python exits.
history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
    readline.read_history_file(history_path)
atexit.register(lambda x=history_path: readline.write_history_file(x))

Note that I have slightly edited it from the version on my blog post so that the IrlCompleter is initialized with a true tab, which seems to be what is output by the Tab key in Terminal.

+2  A: 

This works for me on both Linux bash and OS X 10.4

import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
Van Gale
I've noticed this works in my copy of Python3.0 on Leopard (OS X 10.5), which I compiled against readline 6. It doesn't seem to work for me with the copy of Python2.5 that shipped with the OS.
Jarret Hardie
+1 Because I didn't know about this short way of doing things though!
Jarret Hardie
As Jarrett said, this doesn't work on OS X 10.5 with the included Python 2.5.
gotgenes
Ok that's too bad. I also realized later that this doesn't help with the "initial tab" problem, which is the reason for your more complex code.
Van Gale
@Van Gale: What is the name of the file where I should put the code? I know that you can use .pythonrc by the module user, but I am not sure whether it is the right way or not.
Masi
@Masi: On Linux it is .pythonstartup in your home directory. Unfortunately my Mac hard disk crashed a few weeks ago so I can't verify whether it's the same there.
Van Gale
+3  A: 

To avoid having to use more GPL code, Apple doesn't include a real readline. Instead it uses the BSD-licensed libedit, which is only mostly-readline-compatible. Build your own Python (or use Fink or MacPorts) if you want completion.

vasi
Ah, thanks for explaining why this doesn't work. :-)
gotgenes
It seems that there are no Python with TabCompletion in Macports: % port variants python26 % gives only darwin, universal and ucs4 packages. -- How can you install Python with tab completion by MacPorts?
Masi
+11  A: 

This should work under Leopard's python:

import rlcompleter
import readline
readline.parse_and_bind ("bind ^I rl_complete")

Whereas this one does not:

import readline, rlcompleter
readline.parse_and_bind("tab: complete")

Save it in ~/.pythonrc.py and execute in .bash_profile

export PYTHONSTARTUP=$HOME/.pythonrc.py
B0rG
works for me! Now, can you get it to work with `readline.set_completer()`?
ʞɔıu
works for me on 10.5
ʞɔıu
strange... can you tell me what's your Python version? mine isPython 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
B0rG
yeah, now it works, it was missing import rlcompleter, sorry for making such fuss ;-) cheers.
B0rG
And you can use readline.parse_and_bind ("bind ^[ rl_complete") if you want to bind completion to esc. Which I have bound to caps lock using double-command. That little bit of distance I now save when completing is amazing! (And I use TextMate, which uses esc as the complete key).
Matthew Schinckel
+4  A: 

here is a full cross platform version of loading tab completion for Windows/OS X/Linux in one shot:

#Code  UUID = '9301d536-860d-11de-81c8-0023dfaa9e40'
import sys
try:
        import readline
except ImportError:
        try:
                import pyreadline as readline
        # throw open a browser if we fail both readline and pyreadline
        except ImportError:
                import webbrowser
                webbrowser.open("http://ipython.scipy.org/moin/PyReadline/Intro#line-36")
                # throw open a browser
        #pass
else:
        import rlcompleter
        if(sys.platform == 'darwin'):
                readline.parse_and_bind ("bind ^I rl_complete")
        else:
                readline.parse_and_bind("tab: complete")

From http://www.farmckon.net/?p=181

FarMcKon