CTRL+R in shell - if I go past the command I want, how do I get back to it?

4

1

I'm feverishly hitting CTRL+R to find some command I used awhile ago, when suddenly I get to it. By reflex, I continue hitting CTRL+R, passing it for something further back in the shell history. How do I go back to the command I just passed, without having to start the whole search over?

Andrew

Posted 2016-04-20T18:44:58.513

Reputation: 165

ctrl+s is supposed to search forwards (according to man bash), but apparently that doesn't work on Mint or Ubuntu :/ Bash history search, partial + up-arrow might be relevant for a different solution.

– DavidPostill – 2016-04-20T19:31:25.547

Answers

8

The Ctrl+R functionality to search backwards through shell history is provided by the Readline library used by Bash. The corresponding function to search forwards through the history is, by default, bound to Ctrl+S.

However, the problem is that the terminal driver already uses this key combination for flow control: pressing Ctrl+S stops or pauses text being printed to the terminal and Ctrl+Q resumes the flow of characters sent to the terminal device.

These settings are turned on by default for most terminal devices on GNU/Linux systems with the result that Bash/Readline never sees the Ctrl+S key sequence since it’s caught by the terminal driver and never passed on to the shell.

This behaviour (also termed XON/XOFF flow control) can be disabled by running:

stty -ixon

Now the Ctrl+S key combination will be passed through to Bash/Readline.

Alternative key combination

If you’d prefer Readline to use a different key combination for searching forward, use the bind command to enable a different key binding, e.g. Ctrl+F (for Forward). NB: all the quotes are required.

bind '"\C-f": forward-search-history'

Modify configuration to keep these changes

Terminal flow control isn’t particularly useful on modern devices so I would advocate running the following command whenever a new terminal or pseudo-terminal device is started:

stty -ixon

.bashrc would probably be a good start-up file to run this command as the file is sourced for interactive Bash shells. If .bashrc is not sourced by .profile or .bash_profle, the above stty command should be added to whichever start-up file is used for login shells.

The Readline configuration file is .inputrc if you prefer to use a different key binding such as Ctrl+F to search forward through history:

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

Related links

Anthony Geoghegan

Posted 2016-04-20T18:44:58.513

Reputation: 3 095

What is the purpose of editing the Readline config, if that key combo is the default? – Andrew – 2016-04-21T16:16:47.437

@Andrew When I first wrote that, I wasn't 100% sure that this was the default key combination. I thought it possible that Ctrl-S was a configured by the majority of distribution package maintainers so I included the Readline configuration file for completeness. I'll edit it out as it's not necessary and makes the answer longer than it needs to be. – Anthony Geoghegan – 2016-04-21T18:27:06.260