# Fix Arrow Keys: Do Not Leave Insert-Mode
#
# The terminal sends something like \e[A or \eOA (where \e is the escape key).
# By default (vi-mode):
# 1. \e switches to command mode.
# 2. [A or OA is a "Multi-character binding" and runs an editor command.
#
# The commands bound to the arrow keys (backward-char, up-history, down-history,
# forward-char) are the same as typing h, j, k or l in command mode.
#
# `bindkey -a -s` can be used to chain together multiple editor commands in
# command mode if the editor commands are mapped to keys. For example,
# `bindkey -a -s "[A" "ki" would run both up-history (k) and vi-insert (i).
# So `bindkey -a -s` can be used to re-enter insert mode (i) after running the
# editor commands associated with the arrow keys, since those editor commands
# are mapped to h, j, k and l.
# Up
bindkey -a -s "[A" "ki"
bindkey -a -s "OA" "ki"
# Down
bindkey -a -s "[B" "ji"
bindkey -a -s "OB" "ji"
# Right
bindkey -a -s "[C" "li"
bindkey -a -s "OC" "li"
# Left
bindkey -a -s "[D" "hi"
bindkey -a -s "OD" "hi"
# `bindkey -s` does not seem to work for the arrow keys when in `~/.cshrc`.
# Solution: schedule the commands to run later.
alias fixkeys1 'bindkey -a -s "[A" "ki" ; bindkey -a -s "[B" "ji" ; bindkey -a -s "[C" "li" ; bindkey -a -s "[D" "hi"'
alias fixkeys2 'bindkey -a -s "OA" "ki" ; bindkey -a -s "OB" "ji" ; bindkey -a -s "OC" "li" ; bindkey -a -s "OD" "hi"'
sched +00:00 fixkeys1
sched +00:00 fixkeys2
# Fix Delete Key and Do Not Leave Insert Mode
# (for some reason, this one works, even when included in `~/.cshrc`
bindkey -a -s "[3~" "xi"