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

Tags: , , ,

8 Responses to “Search history in bash”

  1. Perry Lorier says:

    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. Brandon Hale says:

    Going back to

    I find it very useful to map bash history search to Pg Up. Start typing a command, page up to complete it from history. It is much faster for me.

    # alternate mappings for “page up” and “page down” to search the history
    “\e[5~”: history-search-backward
    “\e[6~”: history-search-forward

  3. d says:

    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

  4. Thomas Silvestre says:

    That’s works also in mysql’s command line (is it a feature of readline?).

  5. Tobias says:

    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+

  6. I acutally go the whole hog and do like 4dos used to.
    I.E. have the up/down arrows do the search.
    Note if you don’t type anything the up/down arrow behave as before.

  7. […] Search history in bash – Hitting CTRL-r allows you to search the Bash history by typing a word. It’ll match the word and bring up that command! […]

  8. […] Linuxart » Blog Archive » Search history in bash – I’ve been using command search with the up/down arrow keys in tcsh for ages. Should be a default feature for all shells. Tags: bash history […]

Leave a Reply