3

In most shells on Linux, when you press the Up-arrow key, it shows the previous command lines from the command line history.

My question is, for the ksh shell,

  1. How do I set up some other key so that it shows previous command line?

  2. How do I disable the Up-arrow key so that it doesn't show the previous command line?

  • You might have to look at cat /etc/inputrc – Keyshov Borate Jul 8 '16 at 8:27
  • Many shells provide command line history with the aid of the readline library. To find which shell you are using, run echo $0 -- then edit your question to include the output. – JigglyNaga Jul 8 '16 at 8:43
  • OK, you're using the korn shell. See stackoverflow.com/questions/1623256/… . – JigglyNaga Jul 8 '16 at 9:35
  • ksh? That brings back memories! It used to be my favourite shell (or certainly I don't remember using any shell as much). Like say in the 1990s. That was mostly under SunOS/Solaris. I didn't know people still used ksh (even though I did look at it a few years ago out of curiosity). – Pryftan Jan 15 '18 at 22:38
4

Most tutorials you can find are about bash. Ksh is very good at scripting but is bad at interactive use. Use zsh (best) or bash (second-best) as a command line if you can.

There are two main implementations of ksh today: the actual Korn shell ksh93, and the clone mksh (derived from the long-unmaintained pdksh). They have different key binding facilities.

In ksh93, the Up and Down keys don't move in the command line history by default. The default keys are Ctrl+P and Ctrl+N. You can change key bindings via the KEYBD trap; it's rather clumsy as you can only translate keys to other keys and all the bindings have to be defined in the same place. Here's how to make Up and Down navigate the history (this code goes into ~/.kshrc):

set -o emacs
function KEYBD_trap {
  case ${.sh.edchar} in
    $'\e'[[O]A) .sh.edchar=$'\e>';;
    $'\e'[[O]B) .sh.edchar=$'\e<';;
  esac
}
trap KEYBD_trap KEYBD

\e[A and \eOA, where \e stands for the ASCII escape character, are the two escape sequences that the terminal may send when the user presses the Up key (it depends on the terminal and on its current mode). Likewise \e[B and \eOB is for Down. $'\e is a notation you can use in ksh for the escape character; the pattern [[O] matches either a [ or a O.

In mksh, you define key bindings via the bind builtin. Up and Down navigate the history by default (in addition to kbd>Ctrl+P and Ctrl+N). The following code (to be put in ~/.mkshrc) disables the arrow keys:

bind '^[OA'=
bind '^[[A'=
bind '^[OB'=
bind '^[[B'=

^[ is a notation you can use in mskh key bindings for the escape character.

All this information is in the man page of the shell, not necessarily in a form that's comprehensible if you aren't familiar with Unix shells already.

  • (opinions) For interactive use, I find that shells, like bash and zsh, that tries too hard to second-guess my behaviour (and gets it wrong), or have too many bells-and-whistles, are more a hinderance than a help. ksh is brutally honest with me, like a good friend ;-) – Kusalananda Jul 9 '16 at 10:19

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.