Operating System - HP-UX
1833382 Members
3307 Online
110052 Solutions
New Discussion

Cursors to navigate the ksh command history

 
SOLVED
Go to solution
paolo barila
Valued Contributor

Cursors to navigate the ksh command history

Hi,
is there a way to set vi options in the ksh (set -o vi)
to use cursors to navigate the command history ?
share share share
12 REPLIES 12
Bill Hassell
Honored Contributor

Re: Cursors to navigate the ksh command history

Make sure that in your local profile that HISTFILE has been set and optionally, HISTSIZE. Then touch the history file and logout and back in again. Like this for your local profile:

export HISTFILE=$HOME/.sh_history
export HISTSIZE=1000
set -o vi

Depending on how you setup ksh, you may be using .profile or .kshrc for your local profile. Once set, you can view your history with the history command, recall the last command with ESC k or search backwards with ESC /.


Bill Hassell, sysadmin
Steven E. Protter
Exalted Contributor

Re: Cursors to navigate the ksh command history

Shalom,

Once HISTFILE and HISTSIZE are set you merely hit escape k to go back through history.

There is a command called hist that lets you see and even filter output as well.

Note that HP-UX does not work very well with HISTFILE on NFS.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
paolo barila
Valued Contributor

Re: Cursors to navigate the ksh command history

Sorry I wasn't clear,
I know ESC k
I meant cursors keys -instead- of ESC k
share share share
Ivan Krastev
Honored Contributor

Re: Cursors to navigate the ksh command history

Use keys for navigation like in VI :
h - left
l - right
x - delete
a - append
i - insert


For more use vim help.

;)
paolo barila
Valued Contributor

Re: Cursors to navigate the ksh command history

Hi Ivan,
can you replace

h with cursor key "<-"
l with "->"
...
share share share

Re: Cursors to navigate the ksh command history

Hello Paolo,

This is from the book "Learning the Korn Shell 2nd edition" published by O'Reilly which quotes "The New KornShell Command and Programming Language" published by Prentice Hall.

It only work with KornShell 93 (/usr/dt/bin/dtksh for instance)

These are the lines to enter either in your .profile or at the shell prompt if you want to test it before :

typeset -A Keytable

trap 'eval "${Keytable[${.sh.edchar}]}"' KEYBD

function keybind # key [action]
{
typeset key=$(print -f "%q" "$2")
case $# in
2)
Keytable[$1]=
=' .sh.edchar=${.sh.edmode}'"$key"
;;
1)
unset Keytable[$1]
;;
*)
print -u2 "Usage: $0 key [action]"
return 2 # usage errors return 2 by default
;;
esac
}

Then call the keybind function with arguments that depend on your keyboard settings for the cursor keys. Mine is a PC under MS Windows.

Thus :
- the up arrow key sends ESC[A,
- the down arrow key sends ESC[B,
- the right arrow key sends ESC[C
- the left arrow key sends ESC[D

So I executed the keybind function with the following arguments :

keybind $'\E[A' k
keybind $'\E[B' j
keybind $'\E[C' l
keybind $'\E[D' h

I acknowledge that the code is tricky and I did not understand everything.

Best regards,

JPH

Re: Cursors to navigate the ksh command history

Sorry the line with Keytable[$1]= should be :

Keytable[$1]=' .sh.edchar=${.sh.edmode}'"$key"

There are no two = and it's all on the same line

Best regards,

JPH
paolo barila
Valued Contributor

Re: Cursors to navigate the ksh command history

which trap is KEYBD, my kill -l is
# kill -l
1) HUP 16) USR1 31) RESERVED
2) INT 17) USR2 32) DIL
3) QUIT 18) CHLD 33) XCPU
4) ILL 19) PWR 34) XFSZ
5) TRAP 20) VTALRM 35) bad trap
6) IOT 21) PROF 36) bad trap
7) EMT 22) POLL 37) RTMIN
8) FPE 23) WINCH 38) RTMIN+1
9) KILL 24) STOP 39) RTMIN+2
10) BUS 25) TSTP 40) RTMIN+3
11) SEGV 26) CONT 41) RTMAX-3
12) SYS 27) TTIN 42) RTMAX-2
13) PIPE 28) TTOU 43) RTMAX-1
14) ALRM 29) URG 44) RTMAX
15) TERM 30) LOST
share share share

Re: Cursors to navigate the ksh command history

Paolo,

The trap KornShell built-in does not only catch signals. It can also catch some events like :
- the exit from the shell ( trap command EXIT)
- an error ( trap command ERR )
- the end of a command ( trap command DEBUG )
- a key stroke ( trap command KEYB )

This last event is a KornShell 93 feature not a KornShell 88 nor a POSIX shell (to my knowledge).

If you want to run a KornShell 93 on HP-UX execute /usr/dt/bin/dtksh.

Regards,

JPH
Bill Hassell
Honored Contributor
Solution

Re: Cursors to navigate the ksh command history

Signals (as caught by traps) have nothing to do with the keyboard except as mapped by the stty command. There can be no signal connected to any special key because it does not send a single character. The man pages for termio and stty will help.

AS far as using the arrow keys to navigate the command history, neither standard ksh or the POSIX shell can be configured to use these non-standard keys. Bash has this capability but you would have to download and build it on your system.


Bill Hassell, sysadmin

Re: Cursors to navigate the ksh command history

Hello,

It may be worth glancing at ksh93 features. You probably won't even have to download anything as /usr/dt/bin/dtksh is a ksh93.

Trap does not only catch signals it also intercepts events as I mentionned it previously.

Regards,

JPH
paolo barila
Valued Contributor

Re: Cursors to navigate the ksh command history

10x to all
share share share