I am using putty to connect to the Red hat linux server. My shell under linux in Korn shell. I am used to clearing screen with the "Ctrl + l " combination. Some how this combination is not working under Ksh. If I type "ctrl + l " in ksh, it takes me to the new line.

I tried

alias "^L"=clear

still same result.

I also tried

bind -m '^L'=clear'^J'

but it says bind command not found. I can not install anything new on the machine.

Could you please advise how to fix this issue? Thanks.

  • 1
    What flavor of ksh are you using? mksh, ksh93....? I suppose you took those commands from here : unix.stackexchange.com/questions/10614/… . OpenBSD's ksh might be (I don't know, to be honest) based on a different "flavor" of ksh than Redhat's, so what works in OpenBSD might or might not work in Redhat. – schaiba Mar 28 '16 at 10:39
  • My korn shell is under "/bin/ksh". Yes OpenBSD might no work. I am interested to know what might work in Redhat. Also what is causing ^L to put newline.May be if I am able to find that binding, I can change it, just a thought – Forever Learner Mar 28 '16 at 11:17
  • Why don't you use bash instead? Bash is part of the standard Red Hat installation, you wouldn't need to install anything. – Gilles Mar 28 '16 at 21:41
  • Hi Gilles, the project requirement is for Korn shell. – Forever Learner Mar 31 '16 at 8:17

Red Hat provides "ksh-93", which does not have a bind command. It provides something called KEYBD, which is triggered via a special trap function. According to the manual page, that can be used to modify the Emacs Editing Mode.

The example at [Korn Shell] Fixing Home, End, pgup, pgdown, and delete keys is a useful starting point for showing how to make ^L clear the screen. By default, metacontrolL (^[^L) is bound to the clear-screen function, while plain ^L is just the next-line which you would like to change:

   ^L        Line feed and print current line.
   M-^L      Clear the screen.

Adding just one line to the example does what was asked:

set -o emacs

keybd_trap () {
  case ${.sh.edchar} in
    $'\f')    .sh.edchar=$'\e\f';;  # clear-screen (THIS QUESTION)
    $'\e[1~') .sh.edchar=$'\001';;  # Home = beginning-of-line
    $'\e[4~') .sh.edchar=$'\005';;  # End = end-of-line
    $'\e[5~') .sh.edchar=$'\e>';;   # PgUp = history-previous
    $'\e[6~') .sh.edchar=$'\e<';;   # PgDn = history-next
    $'\e[3~') .sh.edchar=$'\004';;  # Delete = delete-char
trap keybd_trap KEYBD

For whatever reason, vi-mode is far less capable in ksh.

The documentation is vague on how keys are actually mapped to functions. For this instance the feature is implemented in src/cmd/ksh93/edit/emacs.c as part of a switch/case statement:

#ifdef _cmd_tput
                case cntl('L'): /* clear screen */
                        sh_trap("tput clear", 0);

within the escape function (i.e., handling "meta"). So it is built-in behavior that can be customizedksh.

Further reading:

Q4. How is keybinding done?
A4. ksh93 provides a KEYBD trap that gets executed whenever a key
    is entered from the keyboard.  Using this trap, and the associate
    array feature of ksh93, a keybind function can easily be written
    which will map any entered key sequence to another key sequence. 
  • Thanks a lot Thomas Dickey for your detailed explanation. +1 and accepting this answer. Sorry I did not get chance to login to stackoverflow after putting the question. Thanks again :) – Forever Learner Mar 31 '16 at 8:10

Your Answer

