33

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?

2 Answers 2

43

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.

3
  • I run bash in a gnome3 terminal. Your solution works for me, I can see (reverse-i-search) and (i-search) when toggling Ctrl+s and Ctrl+r. How can I enable software flow control back again? stty -ixon does not seem to prevent reverse search from trapping Ctrl+s
    – m-ric
    Sep 12, 2012 at 14:34
  • 3
    You can reenable software flow control using stty ixon (without the hyphen). Sep 13, 2012 at 13:55
  • 1
    Or just rebind Ctrl+C to Ctrl+X keys with $ stty stop ^X From: ruslanspivak.com/2010/11/20/…
    – A.D.
    Apr 5, 2013 at 18:54
0

The terminal interprets the ^S character as its STOP control character instead of passing it on to the shell.

You can change the terminal's STOP character with stty stop ^O for example. To type ^O in your terminal / shell, first type ^V; then type ^O.

To see the terminal's control characters, type stty -a.

References:

  1. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/stty.html#:~:text=shall%20be%20supported%3A-,%2Da,Write%20to%20standard%20output%20all%20the%20current%20settings%20for%20the%20terminal.,-%2Dg
  2. https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#Modifying-Text
  3. https://www.gnu.org/software/bash/manual/bash.html#Commands-For-Text

You must log in to answer this question.

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