15

In Matlab, there is a very nice feature that I like. Suppose I typed the command very-long-command and then a few several commands afterwards. Then later if I need the long command again, I just type very and press the up arrow key, my long command appears. It finds the last command that starts with very. I couldn't do the same in unix command line, when I try to do it, it disregards whatever I typed, and goes back to the last commands in chronological order. Is there a way to do it?

2
  • Matlab has good default settings. I always wonder why the Unix people can't provide good default settings for these things. Commented Jun 19, 2014 at 14:12
  • This question is similar to: How do I change bash history completion to complete what's already on the line?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 22, 2024 at 9:14

3 Answers 3

13

In bash this functionality is provided by the commands history-search-forward and history-search-backward, which by default are not bound to any keys (see here). If you run

bind '"\e[A":history-search-backward' bind '"\e[B":history-search-forward' 

it will make up-arrow and down-arrow search backward and forward through the history for the string of characters between the start of the current line and the point. See also this related Stack Overflow question.

Sign up to request clarification or add additional context in comments.

Comments

8

In bash, hitting ctrl-r will let you do a history search:

$ echo 'something very long' something very long $ # blah $ # many commands later... (reverse-i-search)`ec': echo 'something very long' 

In the above snippet, I hit ctrl-r on the next line after # many commands later..., and then typed ec which brought me back to the echo command. At that point hitting Enter will execute the command.

3 Comments

Thanks! I have another quick question, what if there are two recently used long commands starting with echo, how can I switch between the two with this method?
@AgCl: This blog post explains how to set up history-search-forward and history-search-backward to navigate through the history.
The above link is dead
3

You can do the same thing by using "!". For example:

 $ echo "Hello" Hello $ !echo echo "Hello" Hello 

However, it is generally a bad idea to do this sort of thing (what if the last command did something destructive?). If you expect you will reuse something, then I suggest you create a shell script and save it away somewhere (whenever I plan to reuse something, I create a script in ~/.local/bin).

2 Comments

Thanks, it's almost as close to Matlab's, but I can't see the whole command before hitting enter. I'll upvote the answer when I have enough rep.
!echo:p will display the command (bash anyway) and you can choose to execute it or not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.