Bash – How to have type-ahead apply to bash history search (Ctrl-R)

bashreadlinesttyterminal

Context

Typeahead in bash: good

When a bash shell is busy (initializing, running a command), one can type before the next prompt appears.

If the shell has launched a program, that program will capture the keys, but if no program is run or if the program does not capture input, what one types gets inserted in the shell after prompt appears.

For example : type sleep 5, press Enter, then type ls and press Enter. ls will be run after sleep has finished. In real life, ls would be replaced by cp, rsync or many other programs.
This is a typical Typeahead feature and it's a great time saver when you know in advance what to type.

It's also very nice since it allows to copy-paste several commands and have them run in sequence.

Real-world use case include when the shell takes time to initialize. It could be that the computer is slowed down for any reason, or the shell is on a slow network link, etc.

History search in bash: good

On a bash prompt, one can type Ctrl-R to search through history.

This is an invaluable time saver when reusing some old command lines, or even sequence of command lines. Press Ctrl-R, type a few characters typical of the command to search, press Ctrl-O as many times as needed to replay the recorded commands from there.

Typeahead in history search: how ?

There is one limitation, though. Often I use the sequence above and find that if I type e.g. Ctrl-R ls before the shell prompt has actually appeared, the Ctrl-R part is ignored but the ls part is shown.

The net effect is that one has to wait for the shell prompt to appear before typing Ctrl-R, defeating part of the time saved.

Question

Is there a way to have Ctrl-R honoured even in a typeahead situation ?

Best Answer

Your Ctrl-r is being intercepted by the kernel-based terminal cookied line processing engine.

While sleep is running, the terminal is in cooked mode, which means that the kernel-based tty line editor is working. The tty line editor supports rudimentary command line editing. The erase key (usually set to Ctrl-h (backspace) or Del) and the kill key (usually Ctrl-U) are the best known special editing keys that can be used in this mode. This line editor is useful: it's what lets interactive utilities that use neither readline nor curses to read complete lines of input from the terminal while allowing the user to make typing corrections.

But there's another special key that's active in this mode. You can see it along with the other key settings in the output of stty -a under the name rprnt and its default setting is... you guessed it... Ctrl-r. The function of this key is to repaint the current command line, in case it has become corrupted or misaligned due to other terminal output.

To avoid this, you can disable the function with stty rprnt undef.

Personally I am used to Ctrl-r being interpreted as a repaint command and I am surprised every time I try to do that in bash and it does something different!

Related Question