0

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

bit
  • 1,106

2 Answers2

0

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.

glenn jackman
  • 85,964
  • Ctrlr scrolls forward through lines in the history file containing the text pattern "echo" but Ctrls 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 Ctrls without freezing the terminal 12. Perhaps you could update your post with this caveat and the workaround I mentioned – bit Jul 01 '19 at 19:35
  • Feel free to post your own answer and accept it. – glenn jackman Jul 01 '19 at 22:41
  • You can reverse the history file with tac and do normal search instead of reverse-i. history | tac | less – user9101329 Dec 06 '23 at 11:53
  • @elmo, you should add that as an answer. – glenn jackman Dec 06 '23 at 14:57
0

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.

user9101329
  • 1,004