0

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?

xdavidliu
  • 2,411
  • 14
  • 33
  • 1
    yes I am, but isn't the python interactive shell independent of bash? For example, in bash you can use something like this [link](http://www.gnu.org/software/bash/manual/html_node/Commands-For-History.html), but that's separate from the python shell? – xdavidliu Dec 01 '15 at 22:53
  • I was thinking maybe [this](http://askubuntu.com/questions/59846/bash-history-search-partial-up-arrow), but I don't really have much experience with the shell. – sco1 Dec 01 '15 at 22:54

3 Answers3

1

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.

gboffi
  • 22,939
  • 8
  • 54
  • 85
0

Use iPython or some other Python shell. There are plenty. You may even program your own that will do whatever you want.

Dalen
  • 4,128
  • 1
  • 17
  • 35
  • While I don't disagree with the sentiment, this doesn't answer the question. – sco1 Dec 01 '15 at 22:56
  • ipython does provide TAB completion and command history by pressing up. – Nick Dec 02 '15 at 00:57
  • What sentiment? If you mean that I am in love with iPython you're gravely mistaken. I hate it as a shell. BTW I disagree in general with autocompletion in programming because it tends to make a programmer stupid. However I agree that mathematicians that use Python as a replacement for matlab/octave should have it. I have nothing against code blocks in history though. How comes that my answer doesn't answer the Q??? iPython supports autocompletion and many other shells too. – Dalen Dec 02 '15 at 01:07
  • i appreciate the answer, but in my original question, I specifically mentioned "without Ipython" – xdavidliu Dec 02 '15 at 20:58
  • I am not following edits to see what was written in original question. Sorry. If you hate ipython's output style, you can change it to resemble normal Python shell. Google out Python shells and see which ones support autocompletion. Or write your own. It is not hard. You have cmd, code, IDLELib etc. modules that will help. BTW IDLE supports autocompletion but as I got it, you want CLI shell. There is no other way of having AC then using a shell that supports it. – Dalen Dec 03 '15 at 02:02
-1
  1. tab completion. If I have a function called foobar(...), I can do 'fo' and it will automatically fill in 'foobar'

Really? Which version of Python are you using and how did you get it? It works for me on a regular python shell for both windows and Linux with both python 2.7 and python 3.4. It sounds like your version of Python might not have been built with readline support, which I think is required for this sort of thing.

This is what I tried:

>>> sup

after tab becomes:

>>> super(
  1. "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 ='.

It is called a "History search", and it also works for me in the default Python shell in both windows and Linux. Again, I think this requires readline.

>>> a = 'test'
>>> a

Then I press up, and I get:

>>> a = 'test'

You can also press Ctrl+r, then start typing. This will search the history for the last command that includes what you typed. So, for example:

>>> a = 'test'
>>> b = 5
>>> c = a

Then ctrl+r:

>>>
forward-i-search1`b: b = 5

Then hit Enter to execute that command.

>>>
>>> b = 5
>>>

If the match isn't what you want, you can type more, or hit Ctrl+r again and again to cycle through the matches.

Edit:

It looks like this is a known problem with the built-in Mac Os X version of Python. It doesn't come with readline due to readline being GPL. Instead it includes libedit, which is not fully compatible. There are instructions on how to get it working on Mac Os X here

Community
  • 1
  • 1
TheBlackCat
  • 9,791
  • 3
  • 24
  • 31
  • interesting; I am using the default version of Python that comes with the latest Mac OSX, and neither tab not control-r do anything, and the up button simply cycles through *all* previously used commands, not ones that match what you partially typed. – xdavidliu Dec 02 '15 at 20:57
  • It looks like this is a known problem with the default Mac Os X version of Python. I have edited my answer to point to instructions on how to fix it. It is probably better to put how you installed Python in the OP next time you have a question like this. – TheBlackCat Dec 03 '15 at 08:24