Improved Terminal autocomplete funcionality (similar to Matlab's)

If any of you ever used Matlab, you might have noticed the command window functionality in that program. I wonder if it is possible to enable the same functionality in Mac Terminal.

It works like this: suppose you have used two commands previously in this Terminal session. First you used a cd ~ and then a ls -la.

In every *nix terminal, including Mac's, if you hit the Up Arrow, it'll first show the ls -la command and, if you hit it twice, the cd ~ command. This is great. But, in Matlab, if you first type the letter c and then hit Up Arrow, it won't show you ls, but instead go directly to cd ~. This saves a lot of time when you have big commands and a long list of previously used commands.

Is it possible?


In a Bash shell, you can hit Control+R to do a reverse intelligent search in the history. Start typing the command and the shell will autocomplete with previously entered commands.

Then you can either press Enter to execute the command again, use the arrow left or right key to edit the command, or keep pressing Control+R to cycle through other possible completions.

Reverse intelligent search


Save this as ~/.inputrc:

# make the up and down arrows cycle through commands that match the start of a line
"\e[A": history-search-backward
"\e[B": history-search-forward

Option-up would be \e\e[A and control-p \C-p. You can see the escape sequences of other key combinations by pressing control-v.


Another thing you can do is type ! and then the first letter, and it will find the most recent. IE: cd ~ can be accessed with !c


This is a feature of the shell. Control-R will do what you describe, but changing to a more modern shell will get you so much more. Take a look at Z-shell (zsh), there is a bit of a learning curve, but it is very powerful.