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?
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
functionhistory-search-backward
to a keystroke (andhistory-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
: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 havestty -ixon
in your~/.bashrc
.Note the subtle difference between
history-search-forward
andforward-search-history
.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 !!