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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Niklas Rosencrantz
  • 4,112
  • 6
  • 36
  • 58
  • 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.
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
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
  • 411,918
  • 54
  • 1,065
  • 1,164
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
  • 18,806
  • 5
  • 38
  • 60
  • 2
    AFAIK `tcsh` had a command line editor as far back as 1983 possibly earlier. – Stéphane Chazelas Apr 20 '16 at 13:11
  • ctrl-r originates with Emacs doesn't it? When? 1975? 1983? – Niklas Rosencrantz Apr 20 '16 at 13:57
  • 1
    csh and tcsh had a history but no command line editor at that time – schily Apr 20 '16 at 14:10
  • Correct, it exists in emacs, but not in ksh. – schily Apr 20 '16 at 14:14
  • @Programmer400, [earliest reference I've found on usenet was in 1983](https://groups.google.com/d/msg/net.emacs/XrsmPYYcsqQ/vad4va9x0MEJ). – Stéphane Chazelas Apr 20 '16 at 14:32
  • @StéphaneChazelas Cool! – Niklas Rosencrantz Apr 20 '16 at 14:40
  • 2
    tcsh's 5.7 man page mentions: _Paul Placeway, Ohio State CIS dept., 1983 Added the command line editor._ Confirmed on [his CV](http://www.cs.cmu.edu/~pwp/resume.html) – Stéphane Chazelas Apr 20 '16 at 14:45
  • 1
    Well, in 1987 people asked me whether I could add my command line editor to tcsh. I did not do that because the strcture of the code was really bad, there was no central input routine and so the only way to get the input was to call iotcl(TIOCSTI). If there was an edit at that time already, nobody did ask...Do you have the source code from that time? – schily Apr 20 '16 at 15:13
  • 1
    I just checked his CV and he claims to have written this code between 1983 and 1988. I would assume that this was not available before 1988. – schily Apr 20 '16 at 15:18
  • 1
    I have the source code for 5.4 from 1987 ([from usenet](https://groups.google.com/forum/message/raw?msg=comp.sources.unix/v-q1qRVIpi4/TbAvM_gFD-gJ)) that comes with a rewrite of the line editor, the first version of which was written in 1983 by the same person. – Stéphane Chazelas Apr 20 '16 at 15:19
  • 1
    Well, then it was available somewhere in 1987 after I have been asked to add my editor. Now that you mention this, I remember that a tcsh with editor appeared a short time after I have been asked. – schily Apr 20 '16 at 15:20
  • 1
    [Arnold Robbins (of gawk fame) mentioning working on porting the tcsh line editor to the Bourne shell in 1985](https://groups.google.com/forum/#!search/net.unix$20tcsh/net.unix/Q0TGwmag4e8/7A9pOvSkMw0J). – Stéphane Chazelas Apr 20 '16 at 15:40
  • 1
    [`ksh` and `tcsh` line editing mentioned in 1984](https://groups.google.com/d/msg/net.unix-wizards/bSk7CnERCXU/CWaTREAAk3sJ) on usenet. – Stéphane Chazelas Apr 20 '16 at 15:47
  • 1
    It seems that this version of the Bourne Shell has never been published. I know of two Bourne Shell derivates that include a command line history editor: `ksh` (since 1986) and my Bourne Shell (also known as `bosh`) that added my `bsh`editor from 1984 in 2006. http://schilytools.sourceforge.net/bosh.html If you can point to another working Burne Shell source with integrated history editor, you are welcome. – schily Apr 20 '16 at 15:47
  • 1
    The posting in the usenet from 1984 you mention was after bsh added integrated line editing and ksh used the builtin command `fc` that saved the history to a file and then called `vi` (AFAIK). – schily Apr 20 '16 at 15:50
  • 1
    [this post](https://groups.google.com/d/msg/net.unix-wizards/bSk7CnERCXU/neAu2tL49YkJ) confirms ksh had vi and emacs editing in 1984 – Stéphane Chazelas Apr 20 '16 at 15:51
  • 1
    The ksh READMEs that come with the source mention 1986 as the first year where ksh offered an integrated history editor. – schily Apr 20 '16 at 15:54
  • 1
    http://www.mit.edu/afs.new/net/dev/dev/user/tytso/projects/ksh/ksh.nroff also refers to a `vicmd` shell from 1981. – Stéphane Chazelas Apr 20 '16 at 21:43
  • 1
    `ksh`'s default mode is not `gmacs`. It only is if `$VISUAL` or `$EDITOR` contains `gmacs`. If neither `$EDITOR` nor `$VISUAL` contain `macs` or `vi`, then you get a dumb mode (no line editing). And BTW on Solaris line discipline `^R` redisplays the current buffer like in your `bsh` it would seem. – Stéphane Chazelas Apr 20 '16 at 22:27
  • 1
    I have no idea how you get to that assumption. While ksh88 did not start in edit mode by default, ksh93 does. At least since it has been added to OpenSolaris in 2008. And BTW "line discipline" is something that has gone in 1987 when a modern streams based tty driver was implemented. – schily Apr 21 '16 at 09:36
  • 1
    It only uses `gmacs` on Solaris because of `/etc/ksh.kshrc` there. With STREAMS, the tty line discipline is typically implemented as a STREAMS module. On Solaris, that's the `ldterm` module pushed on serial/pts/console... devices. If Solaris didn't have _line disciplines_, then it couldn't load different disciplines on serial devices like on other systems. In anycase, I was refering to the line editor implemented in that line discipline, and you can't argue that Solaris doesn't have such a line editor. – Stéphane Chazelas Apr 21 '16 at 12:34
  • Actually, Paul Placeway's 1983 tcsh editor was [posted to usenet in 1984](https://groups.google.com/forum/message/raw?msg=net.sources/3eER9GEdo2c/B9G1KGziIx4J), see manual with emacs-like (sort of) key binding. – Stéphane Chazelas Sep 04 '16 at 02:54