Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

up vote 11 down vote accepted

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Add these to your .cshrc (if you are using tcsh) --

  bindkey -k up history-search-backward
  bindkey -k down history-search-forward

Then do source ~/.cshrc to get exactly the functionality you describe.

I don't know if there is a .profile equivalent.

share|improve this answer

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.

share|improve this answer
    
I think you mean Control-R. –  jmk Feb 2 '13 at 20:30
    
Yes I did. Thanks. –  Mark Feb 3 '13 at 0:11

zsh will complete commands exactly like in your example. You don't even have to do anything other than use it for your shell.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.