Search code examples
pythonmatlabpython-interactiveinteractive-shell

python "up-button" command completion, matlab/julia style


I recently switched from Matlab to Numpy and love it. However, one really great thing I liked about Matlab was the ability to complete commands. There are two ways that it does this:

1) tab completion. If I have a function called foobar(...), I can do 'fo' and it will automatically fill in 'foobar'

2) "up-button" completion (I'm not sure what to call this). If I recently entered a command such as 'x = linspace(0, 1, 100); A = eye(50);' and then I wish to quickly type in this same command so that I can re-evaluate it or change it slightly, then I simply type 'x =' then press up and it will cycle through all previous commands you typed that started with 'x ='. This was an awesome awesome feature in Matlab (and if you have heard of Julia, it has done it even better by allowing you to automatically re-enter entire blocks of code, such as when you are defining functions at the interactive prompt)

Both of these features appear to not be present in the ordinary python interactive shell. I believe tab autocomplete has been discussed before and can probably be enabled using the .pythonrc startup script and some modules; however I have not found anything about "up-button" completion. Python does have rudimentary up-button functionality that simply scrolls through all previous commands, but you can't type in the beginning of the command and have that narrow down the range of commands that are scrolled through, and that makes a huge difference.

Anyone know any way to get this functionality on the ordinary python interactive shell, without going to any fancy things like IPython notebooks that require separate installation?


Solution

  • Tab completion is not a standard feature of the python 2.x interpreter. It is possible that a particular distribution (intending, Linux distribution) ships with initialization files that enable tab completion. On the other hand, python 3.x has autocompletion enabled by default.

    To enable tab completion in 2.x, you need to instruct the interpreter about loading some startup code, using an environment variable

    export PYTHONSTARTUP=$HOME/.whatever
    

    The code that you want to put into the startup file varies, but for enabling tab completion the docs have

    try:
        import readline
    except ImportError:
        print "Module readline not available."
    else:
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    

    Coming eventually to your ast question, what you named “up-button” command completion, matlab/julia style, IPython has it and I'm not aware of a module that implements it, even if it seems to me that I read something on such a beast on comp.lang.python some month ago.

    In your question you reference IPython's notebook... It may be necessary to remind that you don't need the notebook interface to use IPython, it can be used to its full potential even in a text console.