3

If I use PC-BSD with the default shell (Korn) then Ctrl+r doesn't work. Why won't it work?

Ctrl-r was introduced to search your history in the late 1970s or early 80s and my BSD still can't do it (while Ubuntu can).

Ctrl-r originates with Emacs doesn't it? When? 1975? 1983?

  • 1
    http://unix.stackexchange.com/questions/541/best-way-to-search-my-shells-history -- please see here . – schaiba Apr 20 '16 at 12:38

3 Answers3

11

Ctrl+R works with ksh in emacs mode (ksh -o emacs or set -o emacs within ksh), and it was most probably the first shell to support it. Only it's not as interactive as in zsh or bash or tcsh's i-search-back widget.

In ksh (both ksh88 and ksh93), you type Ctrl+RtextReturn. And Ctrl+RReturn to search again with the same text.

In vi mode, you can use ? to search backward and n for next search.

That emacs incremental search feature was added to:

  • bash/readline at least since July 1989 as the feature was already mentioned on usenet at that time but probably not from the start as the version of readline shipped with zsh-1.0 didn't have it.
  • zsh since 2.0 in 1991 after the line editor was rewritten and no longer used readline.
  • tcsh in V6.00.03, 10/21/91, but not bound by default (tcsh had other search mechanism on Meta-P for a while before that though).
  • ksh: ksh was most probably the first Unix shell to have an emacs editing mode, written in 1982 by Mike Veach (as well as the vi mode by Pat Sullivan, reusing code that those two had already applied independently to the Bourne shell) at AT&T. ksh was first introduced outside AT&T at the 1983 USENIX conference where those features were described, but was not commercially available until some time after that (1, 2). It's hard to tell if ^R was already there at the time (in any case, it was already there in 1986 and 1985 (see usr/man/man1/ksh.1 ksh85 man page in that Unix V8 tarball at the Unix Heritage Society)), but it's hard to imagine it wasn't as it's an essential feature, especially for a shell, and I'd expect vi mode's ? would also have been there at the time.
6

The Korn shell does support history searches using CtrlR, at least since ksh93 (and perhaps even ksh88), but they don't work quite like bash. First you need to enable Emacs mode:

set -o emacs

(This enables other niceties, such as arrow keys working by default.)

Now if you press CtrlR, the shell will print ^R; type your search, hit Enter, and the shell will show you the closest matching history entry. You can hit Enter again to run it as-is, or edit it.

Stephen Kitt
  • 434,908
0

In the 1970s there was no cursor editable history for shells.

The first shell with integrated cursor editable history was my bsh in 1984 (based on a prototype from 1982). This history implementation uses crontrol-r to redisplay the current commandline.

In 1986, the Korn Shell became a member of the group of shells that implement a fully integrated cursor editable history.

A tcsh version with integrated editor first appeared in 1987.

Bash appeared in 1989.

Ksh by default does not run in emacs mode but rather in gmacs mode.

Note that ksh93 does support searching with control-r even in default mode, it just does not tell you that it is in that mode. If I type:

^R

I get this echoed and then can enter a search string:

^Rali
alias a=b

it finds the alias command in the history.

schily
  • 19,173