30

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.

Sparkler
  • 1,059

3 Answers3

47

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.

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

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

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

  1. Use the history command. As @Isaac explained, that will list all of the commands stored in your history file.
mbomb007
  • 123
terdon
  • 242,166
  • 3
    #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
  • @Sparkler thanks but neither seems relevant. They both affect tab completion, not history. The second is not even a good idea depending on how you use your machine (I don not want my shell completion to be case insensitive!) – terdon Oct 10 '14 at 18:01
  • In case of #4 answer (the most useful to me), use GNU screen or tmux to open another window in case of a typo in your bash setup files... Then exec bash -l to restart your bash without closing your current screen or tmux window. – Joel.O Apr 30 '17 at 11:54
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.

Isaac
  • 392
3

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.

Brian
  • 131