The contents of the command history file using history | less is:

555 ls
556 ls -a
557 echo "hello"
558 echo "hello again"
559 cd
560 pwd
561 echo "hello hello"
562 ls
563 echo "hello hello hello"
564 cd
565 pwd

I want to use reverse i search (reverse-i-search)`echo': to cycle through only commands in the command history file matching the pattern (i.e. in this case `echo') entered on the command line prompt, preferably until a specified line number in the command history file so that I only select previously used echo commands that I want to run again not all echo commands in the command history file

Using reverse i search finds the most recent usage of the command echo (hence the name reverse i search i.e. reverse searching for a command). However when scrolling forward in reverse order (i.e. reverse searching) bash scrolls through each line of the command history file starting from the line returned by the reverse i search, thus including commands I don't want to search for.

  1. The reverse i search prompt (reverse-i-search)`echo': disappears once you start scrolling through the history list. How do you prevent the reverse i search prompt from disappearing during scrolling so that when you scroll forward in a reverse i search only the commands matching the pattern are shown?
  2. If 1 is possible then how do you temporarily limit the reverse i search to a specific line number in the command history file in order to only select commands from a section of the command history file?

Note: a solution to this question would also be useful for those times when you have several instances of the same command line arguments and reverse i search does not find the one you're looking for

  • Which shell are you using?
    – Philippos
    Jun 26, 2019 at 10:37
  • @Philippos bash
    – bit
    Jul 1, 2019 at 20:30

2 Answers 2


Once you have entered your search term, use:

  • Ctrlr to search "up" for the previous command, and
  • Ctrls to switch direction and search "down" for the next one.

I have no idea how to limit the number of history entries to scan.

  • <kbd>Ctrl</kbd><kbd>r</kbd> scrolls forward through lines in the history file containing the text pattern "echo" but <kbd>Ctrl</kbd><kbd>s</kbd> freezes the terminal for me. It seems I need to put the command stty -xion in the start up script ~/.bashrc to be able to step forward in matching command lines using <kbd>Ctrl</kbd><kbd>s</kbd> without freezing the terminal 12. Perhaps you could update your post with this caveat and the workaround I mentioned
    – bit
    Jul 1, 2019 at 19:35
  • Feel free to post your own answer and accept it. Jul 1, 2019 at 22:41
  • You can reverse the history file with tac and do normal search instead of reverse-i. history | tac | less
    – dhm
    Dec 6, 2023 at 11:53
  • @elmo, you should add that as an answer. Dec 6, 2023 at 14:57

If you must use less command for history search, then a reverse search can more easily be done using tac command:

$ history | tac | less

This way you won't need to use reverse-i option in less.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .