Using BASH, you can get the last command (when connected through a PuTty terminal) using the UP arrow key.

I want to know if there's a way to "autocomplete" the last command I did starting with mail, for example.

In Google Chrome, for instance, the way the address bar works, by narrowing down from your previous web history the site you are trying to visit.

So what I want to do is start to type "mail", then press the UP key for example, and the last command starting with mail gets autocompleted. If I press up again, then the 2nd last command starting with mail get autocompleted.

Is this possible in BASH?

4 Answers 4


Hit CTRL + R it will start the reverse history search. If you want a second command with the string you wish to find, continue hitting CTRL + R until you find the command with what you want.

You can also use the history command to see the last commands you have entered.


You can bind the readline function history-search-backward to a keystroke (and history-search-forward to another). I have them bound to PgUp and PgDn which on my system are ^[[5~ and ^[[6~ so I have the following in my ~/.inputrc:

"\e[5~": history-search-backward
"\e[6~": history-search-forward

Using that, you type the beginning of the line you want, mail for example, then press PgUp repeatedly until you get the previous line that you're looking for. You can press PgDn to step back forward through the list. The difference with Ctrl+R is that with it you press the key first then it matches any string anywhere within the line rather than only at the beginning. I find that I use both methods depending on what I'm doing.

You can step back forward after pressing Ctrl+R a few times by pressing Ctrl+S if you have forward-search-history bound to it and you have stty -ixon in your ~/.bashrc.

Note the subtle difference between history-search-forward and forward-search-history.

  • How did you do the key things? For the buttons. Aug 23, 2010 at 13:29
  • 1
    @freedom: <kbd>Key</kbd> Aug 23, 2010 at 13:58

One way to execute the last command beginning with mail is !mail but it won't let you scroll up, it's last or nothing (method above is better :))


You can execute the last command with !!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .