21

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.

  • 4
    If you have brought the command up with Ctrl-r, then you only need to press Enter to execute it... – jasonwryan Oct 10 '14 at 8:09
  • 1
    the question appears to be a duplicate of this, this and this. – Sparkler Oct 10 '14 at 17:09
30

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:

  1. Use its number. If you know that the command you want was 3 commands ago, you can just run

    !-3
    

    That will re-execute the command you ran three commands ago.

  2. 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.

  3. 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.

  4. Add these lines to your ~/.inputrc (create the file if it doesn't exist):

    "\e[A": history-search-backward
    "\e[B": history-search-forward
    

    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.

  5. Use the history command. As @Isaac explained, that will list all of the commands stored in your history file.

|improve this answer|||||
  • 2
    #4 is exactly what I needed. #2 is also cool, but #4 comes more natural to me. – Sparkler Oct 10 '14 at 15:27
  • oh, I missed your answer #2 ... I'll delete my answer – Olivier Dulac Oct 10 '14 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) – Olivier Dulac Oct 10 '14 at 17:01
  • it's also possible to first type ABC and then press [ctrl]+[r], which might me more natural for some users – Sparkler Oct 10 '14 at 17:04
  • on askubuntu they also recommended adding set show-all-if-ambiguous on and set completion-ignore-case on to ~/.inputrc. – Sparkler Oct 10 '14 at 17:13
8

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.

|improve this answer|||||
2

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.

|improve this answer|||||

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