Is there a key combination for bash to auto compete a command from the history? In ipython and matlab for example this is achieved by pressing up arrow after typing a few characters.
First of all, hitting tab in bash is even better since it autocompletes all executables in your PATH irrespecitve of whether they're in the history. That said, there are various ways of getting a command from your history:
Use its number. If you know that the command you want was 3 commands ago, you can just run
That will re-execute the command you ran three commands ago.
Search for it. Type Ctrlr and start typing any text. The first command from the history that matches your text will be shown and hitting enter will execute it.
Hit ▲ (up arrow). That will bring up the last command, press it again and you will go up your command history. When you find the one you want, hit enter.
Add these lines to your
~/.inputrc(create the file if it doesn't exist):
"\e[A": history-search-backward "\e[B": history-search-forward
To immediately load the file, run
bind -f ~/.inputrc (source). Now, type the first few characters of one of the commands you've previously run and hit ▲. The first command from your history that starts with those characters will be shown. Hit ▲ again to see the rest and hit enter when you've found the one you want.
- Use the
historycommand. As @Isaac explained, that will list all of the commands stored in your history file.
3#4 is exactly what I needed. #2 is also cool, but #4 comes more natural to me. Oct 10, 2014 at 15:27
oh, I missed your answer #2 ... I'll delete my answer Oct 10, 2014 at 16:59
#2 matches anywhere in the history line, makes it very nice to find "the few last commands where I operated on THISSERVER" : THISSER[ctrl+r] (and additionnal Ctrl+r to find the older occurences in your history) Oct 10, 2014 at 17:01
it's also possible to first type ABC and then press [ctrl]+[r], which might me more natural for some users Oct 10, 2014 at 17:04
You can check the command position on the history sequence with the command
history. Then you can execute
!<number_in_the_sequence>. I hope that helps.
You can also use
history and pipe the output for a specific command.
For example, if you've been
cd-ing all around the file system all day and just need a reminder of what directories were worked on
history | grep cd
You could then either copy/paste the command, or
!n (where n is the line number in history file) to re-execute it.
Ctrl-r, then you only need to press
Enterto execute it...