Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer

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.

share|improve this answer
How did you do the key things? For the buttons. – David Rickman Aug 23 '10 at 13:29
@freedom: <kbd>Key</kbd> – Dennis Williamson Aug 23 '10 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 :))

share|improve this answer

You can execute the last command with !!

share|improve this answer

Your Answer


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.