Search history in bash

This is another quick tip: to search typed history at a bash prompt, hit control-r and start typing a command you used previously. It doesn’t have to start with what is typed, as it returns matches which contain the searched text.

Hit control-r again (while searching), and it will jump back further in history and search with the same text.

Hitting enter when bash finds something will run the command while hitting the arrow keys will allow you to move around and edit the command. The escape key will also exit the search and place the historical command on your command line. To cancel the search entirely, use control-c and you’ll be returned to a clean prompt (although the matching string will be presented above where you’re typing).

8 thoughts on “Search history in bash

  1. Perry Lorier

    Even better, use ^R to search for a command, then hit ^O on it, bash will execute the command (as if you pressed enter) then put the *next* command on the command line ready for you to use. Press ^O again and it’ll execute that command and show the *next* command and so on. Very handy for vi/make/execute/vi/make/execute style commands.

  2. d

    One thing that bash is sorely is dabbrev-expand (bound to M-/ in tcsh and emacs)
    with it you can complete stuff that was on a previous command line, very useful
    when you have long file names/commands that appeared on different previous command

  3. Tobias

    Funny, I wanted to make the same recommendation.

    “\e[5~”: history-search-backward
    “\e[6~”: history-search-forward

    has a unique power feature distinct from ^R: It completes line beginnings from history. And it’s faster as Brandeon says.

    You can use alt+> or alt+

  4. Pingback: Holy Shmoly! :: Bash Fun - history and navigation

  5. Pingback: Links for Wed 11 Jul 2007 | Joseph Scott's Blog

Leave a Reply