7

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 ?

4
  • seems to work for me with sleep. bash 4.3.39
    – meuh
    Aug 31, 2015 at 9:45
  • Observed on Debian and Ubuntu systems for ages. Based on Celada's answer, I guess it's a matter not of program versions but on terminal settings. Aug 31, 2015 at 11:05
  • You are right. I found stty rprnt undef in my .bashrc. According to my rcs log I added it in 16 years ago, and, sadly, I'd forgotten all about it...
    – meuh
    Aug 31, 2015 at 11:37
  • 1
    Workaround: while the other command is running and the terminal is in cooked mode, type Control-V Control-R. The Control-V is used by the operating system to quote the following character; the effect is to enter the Control-R literally into the input stream. Aug 31, 2015 at 17:34

1 Answer 1

15

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!

2
  • 2
    Wow, high quality answer. Explain what happens, why, how to confirm, how to get what's asked, the side-effects, why one would prefer not to change anything. Both thumbs up! Aug 31, 2015 at 11:01
  • Indeed rprnt = ^R; appeared on stty -a before the fix, not after. I've never seen any added value of Ctrl-R in cooked mode, now I do. I might assign it to something else then. Thanks again. Aug 31, 2015 at 11:03

You must log in to answer this question.

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