1

This is a request for explanation or pointer to documentation.

I'm a KornShell93 (ksh on Mac OS X or ksh93 elsewhere) user. I like the shell for its relative simplicity as an interactive shell and for its scripting capabilities, and I've been using it since the code was made more free in 2000. I use it in vi editing mode, with both the vi and viraw options set.

There's a neat feature in ksh93 that allows you to start typing a command on the command line and then press Up-arrow to get the latest matching command line out of the command line history. I can find the documentation for this and how it's bound to [count][A in vi editing mode, no problem.

However, if I press Up-arrow at an empty prompt, it first gives me the most recently executed command, then it starts cycling through whatever command prefix I had previously looked for. This annoys me no ends.

Example:

I execute these three commands:

$ ls -l
$ ls -ld test
$ ls -ld testdir

Typing ls and then pressing Up-arrow three times will cycle through them in opposite order.

Later, with many more commands issued, but no history searches and no ls invocations:

$ cc -o testrun mytest.c
$ man strncmp

If I now press Up-arrow once, I get man strncmp. If I press Up-arrow again, I get ls -ld testdir and then ls -ld test rather than the command cc -o testrun mytest.c and then the previous before that.

I'm looking for pointers for how to "fix" this so that Up-arrow without typing anything on the command line beforehand cycles through the most recent history without trying search for a previously used prefix. Even a pointer to somewhere where the behaviour above is documented would be nice.

EDIT:

The ksh93 manual says

[count]k Fetch previous command. Each time k is entered the previous command back in time is accessed.

/string Search backward through history for a previous command containing string. String is terminated by a RETURN or NEW LINE. If string is preceded by a ^, the matched line must begin with string. If string is null, the previous string will be used.

[count][A If cursor is at the end of the line it is equivalent to / with string set to the contents of the current line. Otherwise, it is equivalent to k.

This does not explain why I first get man strncmp (as if pressing k) and then get ls -ld testdir in my example above. With the text of the manual in mind, I would expect to get ls -ld testdir on the first press of Up-arrow (which would be even more annoying, by the way).

  • sounds like a bug. report it to whoever maintains the version of ksh93 that you're using. – cas Jun 25 '16 at 8:32
  • 1
    I can confirm this appears to be normal behaviour for ksh93 when in Vi Editing mode. Up-Arrow performs a search, while [ESC] [k] [k] ... scrolls back through history until you hit the top. – Henk Langeveld Jun 26 '16 at 10:41
2

Turns out it is documented behaviour. From the ksh93 man page:

Search Edit Commands These commands access your command history.

[count]k Fetch previous command. Each time k is entered the previous command back in time is accessed.

[count]- Equivalent to k.

[count][A If cursor is at the end of the line it is equivalent to / with string set to the contents of the current line. Otherwise, it is equivalent to k.

[count]j Fetch next command. Each time j is entered the next command forward in time is accessed.

/string Search backward through history for a previous command containing string. String is terminated by a RETURN' orNEW LINE'. If string is preceded by a ^, the matched line must begin with string. If string is null, the previous string will be used.

Note: When using Up-Arrow after entering a string, the search string will be anchored to the start of the line, as if ^ was typed in front.

Example:

$ ls[Up-Arrow]

is the equivalent of

$ [ESC]/^ls
  • 1
    In all these years I've haven't seen that passage!! And I've read the manual several times a month. Big thanks! – Kusalananda Jun 26 '16 at 11:15
  • Actually, now I wonder if I'm seeing the described behaviour. Why do I first get the previous line (as if k) and then a history search on the previous pattern? – Kusalananda Jun 27 '16 at 8:34
  • There's no overlap with that previous line? What version of ksh93 is this? Try print -v .sh.version - results in Version AJM 93u+ 2012-08-01 on my Ubuntu 14.04 – Henk Langeveld Jun 27 '16 at 22:22
  • Version AJM 93u+ 2012-08-01 is the one shipped with Mac OS X too, and it's the same under RedHat and OpenBSD (same ksh version). The prompt is fresh, and then I press up-arrow. I'd expect that it would behave as /string with a null string since the cursor is at the end (and beginning) of the line, but instead it behaves as k and then as /string with null string. – Kusalananda Jun 28 '16 at 5:11
  • 1
    Confirmed. I see the same behaviour now. Looks like a real bug. – Henk Langeveld Jun 28 '16 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.