Linux – How to reverse-i-search back and forth

linuxsearch

I use reverse-i-search often, and that's cool. Sometime though when pressing Ctrl+r multiple times, I pass the command I am actually looking for. Because Ctrl+r searches backward in history, from newest to oldest, I have to:

cancel,
search again and
stop exactly at the command, without passing it.
While in reverse-i-search prompt, is it possible to search forward, i.e. from where I stand to newest. I naively tried Ctrl+shift+r, no luck. I heard about Ctrl+g but this is not what I am expecting here. Anyone has an idea?

Best Answer

The one-sentence answer: Run stty -ixon in your terminal, and then use Ctrl+S to change the search direction from backward to forward.

The exact answer likely depends on which shell you're using. However, in both bash and Zsh (and probably other shells), you can (in theory) type Ctrl+S to search forward. Thus, after hitting Ctrl+R a few too many times, you can hit Ctrl+S a few times to move to the command you actually want. Many terminals, however, trap Ctrl+S and Ctrl+Q for software flow control, so the Ctrl+S never makes it to bash; if you want bash, rather than your terminal, to read the Ctrl+S, you can disable software flow control by running stty -ixon.

Related Question