12

I use Ctrl+R all the time, but I often end up going past the command I'm after as I'm pressing it so quickly.

To forward search, Ctrl+S can be used provided it's not used the terminal first (konsole in my case, in which stty -ixon in ~/.bashrc fixes it).

However, I have to press it twice: once it seems to enter i-search after being in reverse-i-search mode and a second time to actually step backwards.

Is there a way to remove the need for pressing the shortcut twice?

  • 3
    As with Ctrl-R, the first Ctrl-S waits for a string to search. The second one reuse the same search string you used before (with Ctrl-R in your situation). I know of no way to avoid those two steps. readline lacks a "search the same string the other way round" function. – xhienne Mar 25 '17 at 1:30
7
+50

Here's a different approach. If you are comfortable with some basic vi editing commands, bash supports a vi-mode for command line editing. If you really hate vi you won't like this. But if you can tolerate it, you may find it preferable and with fewer keystrokes.

set -o vi

History search works like this:

  • Esc to enter command mode
  • / to begin search
  • Type text of search string
  • Enter to perform search
  • n to go to next match
  • N to jump back to the previous match
  • i to get back into insert mode
  • Enter to run command
  • The vi-style equivalents to Ctrl-R and Ctrl-S are n and N, respectively. And they don't require tapping twice. Intriguing! – Nathaniel M. Beaver May 27 '17 at 23:41
2

Apparently the Bash history search requires a first press of the shortcut to let you know which direction you're searching, then a second press to actually perform the search. I don't think is possible to change the way it works without modifying the source code.

However, you can install hstr (https://github.com/dvorka/hstr) which features a suggest box with advanced search options to easily view, navigate, search, and manage your command history:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.