Remapping keys for ksh vi mode
In my .vimrc
file I have the line
inoremap jj << Esc>>
(I can't live without this :))
I wish to ha开发者_开发知识库ve the same remap while using ksh in set -o vi
mode, any advice on how to do this is appreciated.
Give this a try:
_key_handler () {
# by Dennis Williamson - 2011-01-14
# for http://stackoverflow.com/questions/4690695/remapping-keys-for-ksh-vi-mode
# 2011-01-15 - added cursor color change
typeset timeout=1 # whole seconds
# the cursor color change sequences are for xterms that support this feature
if [[ $TERM == *xterm*color* ]]
then
typeset color=true # change cursor color when chars are held
# cursor colors - set them as you like
typeset nohold="\E]12;green\a" hold="\E]12;red\a"
else
typeset color=false
fi
if [[ ${.sh.edmode} == $'\x1b' ]] # vi edit mode
then
case ${.sh.edchar} in
j)
if [[ $_kh_prevchar == j ]]
then
if (( $SECONDS < _kh_prevtime + timeout ))
then
.sh.edchar=$'\E' # remapped sequence
_kh_prevchar=''
$color && printf "$nohold"
fi
else
_kh_prevchar=${.sh.edchar}
.sh.edchar=''
$color && printf "$hold" &&
# jiggle the cursor so the color change shows
tput cuf1 && sleep .02 && tput cub1
fi
_kh_prevtime=$SECONDS
;;
*)
if [[ -n $_kh_prevchar ]]
then
.sh.edchar=$_kh_prevchar${.sh.edchar}
fi
_kh_prevchar=''
$color && printf "$nohold"
;;
esac
fi
}
trap _key_handler KEYBD
set -o vi
Put it in a file, ~/.input.ksh
for example, and source it from your ~/.kshrc
or similar.
Pressing "j" will put it on hold. If the time runs out before pressing another "j", the first "j" will be output and the next one will be held. If another key besides "j" is pressed, the held "j" and the next character are output together. If a second "j" is pressed before time runs out, the remapped sequence will be output.
Example: Pressing "j", pause, then press "jj" will yield no response at first then "j<< Esc>>" all at once.
A difference between this and vim
is that vim
will go ahead and output the held character after the time runs out even if another key hasn't been pressed yet. Also, the timeout in this is in whole seconds, while in vim
it's in milliseconds.
I've only tested this a little and only with ksh93.
Edit: Added cursor color change.
精彩评论