43

Bash offers the functionality to reverse search via Ctrl + R. Then one can type in a part of a command it will show a fitting entry from the history.

Assume this is my history:

vim foo1
vim foo2 # I want to go here
vim foo3 # this is where I land, how to go back?

I search for foo. Hitting Ctrl + R again shows the next fitting search entry. Often it happens to me, that I am too fast and navigate past my intended result and vim foo3 is shown and now I want to go back to vim foo2.

Hence my question is: How do I navigate within the reverse search?

k0pernikus
  • 15,463

2 Answers2

37

You can access this via the forward-search-history function which is bind per default to ctrl+s. Unfortunately ctrl+s is used to signal xoff per default which means you can't use it to change the direction of the search. There are two solutions for solving the problem, one disabling sending the xoff/xon signaling and the other change the keybinding for forward-search-history

Disable xon/xoff

Run stty -ixon in your terminal or add it to your ~/.bashrc. This allows you to use ctrl+s to use the forward-search-history history function.

For more information about control flow have a look at How to unfreeze after accidentally pressing Ctrl-S in a terminal? and some of the answers

Change the keybinding

If you don't want to change the default behavior of ctrl+s you can change the keybinding for forward-search-history with bind. As most keys are already defined in bash you may have to get creative:

bind "\C-t":forward-search-history

This will bind ctrl+t to forward-search-history, but please be aware that per default ctrl+t runs transpose-chars

Ulrich Dangel
  • 25,369
  • 1
    I tried your first solution stty -ixon in zsh and it works very well. However are there any possibly unwanted side effects in disabling the xoff/xon signaling? – student May 31 '12 at 09:18
  • 1
    @student You can't use ctrl+s for pausing the terminal output any longer. If you never used it before this is no problem – Ulrich Dangel May 31 '12 at 14:24
  • 1
    For what it's worth, I went with the second solution. Great stuff ! I never knew you could freeze the console by pressing C-s.. very useful when using tail on server logs. Neat ! (I wish I had another upvote :) ) – Ashutosh Jindal Nov 20 '12 at 09:37
  • I couldn't get option #2 to work on OSX. I had to do this instead: bind '"\C-t": history-search-forward' – stiemannkj1 Mar 26 '15 at 17:14
1

As an alternative to the basic reverse search and forward search provided by your bash, you may want to look into:

fzf, a self-described "command-line fuzzy finder".

It can replace the default reverse search of your bash (and other terminals).

Its benefits are that it:

  • prints a list of your commands from history
    • toggles chronological or relevance sorting by hitting Ctrl + R again
  • can be navigated via arrow keys (or Ctrl + J / Ctrl + K)
  • allows searching for multiple needles

My workflow now completly relies on fzf over the basic reverse search on machines I have control over.

k0pernikus
  • 15,463