Jan 23, 2013

f Comment

[SOLVED] What's the Unix Hot Key to Search For a Previously Entered Command?

Amazon Let's say you'd like to use a Unix command you've previously entered and you do NOT want to type the same command again because it's long and hard to remember. What do you do other than keep hitting 'up' arrow?

Turns out you can enter the following hotkey in the Unix prompt to search for an old command:

Control and R pressed at the same time
Here's a sample run of this hot key on my Unix box as I type 'tail':

(reverse-i-search)`tail': tail -f /home/ubuntu/webserver-log/access.log

As you can see I only typed the first part of the target command and I get to see a previous command that matches it, which is 'tail -f /home/ubuntu/webserver-log/access.log'. Then I hit 'enter' to execute this command. Or I hit right arrow key to edit this command.

Once you are in this mode start entering a part of the target command as hints. For example if you've entered 'tail -f /usr/local/bin/a/b/c/d/e/access.log' a while ago and you'd like to run this command now, you type Control-r to get into the search mode, and then start typing 'tail -f' and you should see the matching command popping out as you type more.

In this mode, if you type Control-r you will go to the next matching command without entering further hint. For example let's say you've run 'tail -f a.txt' and 'tail -f b.txt' before. When you are in search mode and start typing 'tail -f' you may see 'tail -f a.txt' or 'tail -f b.txt' show up. Let's say you want to run the other command. Simply type C-r to go to the next command that matches your inputted hint.

I've used this shortcut key on many Unix platforms and have never failed, including Ubuntu 10.04.4 LTS.
For more useful hot keys and shortcuts please read Learn The Most Useful Hot Keys and Shortcut Keys in the Unix Command Prompt!

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael