Please forgive my ignorance.... I have a history of commands in Bash on OS X and Linux. At times, I want to go back to an item based on the command. For example, suppose I previously entered:

make distclean && make dist && cp *.diff *.tar.gz ../

Then, suppose I have tens or hundreds of commands following it in history.

How can I type make and then use, say, the up arrow () or control and up arrow (CTRL ) to go back to that command in history?

The "stub" or "stem" means I only type "ma", and not "make".

I work in Bash on both Linux and OS X, so I'd be interested in both operating systems.

  • 1
    You want to do: Ctrl-R, type make and then hit Ctrl-R until you find the make command you want to re-do. – glenn jackman Oct 8 '15 at 19:50
  • @Glenn - you should answer. I just verified CTRL+R works on both. (Its not as elegant as I would like, but it works well. For some reason, I thought I could type make and the CTRL + ). – jww Oct 8 '15 at 21:32
  • The fish shell does this /very/ well, but it is not bash. I'd recommend taking a some time to read the bash manual and learn the various history mechanisms – glenn jackman Oct 8 '15 at 23:42

Alternatively to Ctrl-R, you can use the history command and pipe it to grep for a specific command.

$ history | grep vi 
    7  vi /etc/shells
    8  sudo vi /etc/shells

From there, you can see the history number, and can run that command again using !number.

$ !8
sudo vi /etc/shells

And I believe as long as you don't fill up or clean out the command history, that number should remain the same.


Here are few useful tricks which help you to repeat the command based on previous commands (apart of using Ctrl+R):

  • use !name to repeat last command starting with name, e.g.:

  • to repeat command (e.g. with sudo), use:

    sudo !!
  • to repeat argument, use !$:

    ls /usr/local/bin
    cd  !$
  • to edit the file from the last argument, use !^:

     cp /etc/hosts hosts.bak
     vim !^

See more at: How To Use Bash History to Improve Your Command-Line Productivity

Also see: What are your favorite command line features or tricks? at Unix SE

As a side-note, if you're making typos too often, the is an app for that:


There is a readline (not bash) feature that does exactly that. Add :


to your .inputrc file.
Unlike Ctrl-r it only matches on the prefix. See The most important command-line tip - incremental history searching with .inputrc for more details.

  • That looks promising (assuming "\e[A" is like CTRL + ). Unfortunately, I don't have an .inputrc on either OS X or Linux. When I added it to .bash_profile on OS X (and then source'd it), it resulted in -bash: \e[A:: command not found. – jww Oct 9 '15 at 23:00
  • readline is the library used by bash to handle the history, (unless the --noediting option is used). It has it's own configuration file, usually ~/.inputrc (user), /etc/inputrc (global) or the file declared in the INPUTRC variable. There is a whole section about readline in the bash man page. There is quite a few interesting configuration options – bwt Oct 10 '15 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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