zsh (z shell) numpad/numlock doesn't work

The numpad key do not send the same key symbols as the 'normal' number key in the top row. For example: The numpad-1-key sends KP_1 while the 1-key just sends 1.

Some terminals automatically remap the numpad key to send the same codes to the shell. Also, some shells (for example bash) just interprete the numpad keys like their equivalents in the main block.

zsh does not do the mapping automatically, but you can use bindkey to do the mapping on your own. I have the following in my ~/.zshrc to get the keypad working:

# Keypad
# 0 . Enter
bindkey -s "^[Op" "0"
bindkey -s "^[Ol" "."
bindkey -s "^[OM" "^M"
# 1 2 3
bindkey -s "^[Oq" "1"
bindkey -s "^[Or" "2"
bindkey -s "^[Os" "3"
# 4 5 6
bindkey -s "^[Ot" "4"
bindkey -s "^[Ou" "5"
bindkey -s "^[Ov" "6"
# 7 8 9
bindkey -s "^[Ow" "7"
bindkey -s "^[Ox" "8"
bindkey -s "^[Oy" "9"
# + -  * /
bindkey -s "^[Ok" "+"
bindkey -s "^[Om" "-"
bindkey -s "^[Oj" "*"
bindkey -s "^[Oo" "/"

bindkey -s in-string out-string binds in-string to out-string. If in-string is typed out-string is pushed back and treated as input.

The actual codes (for example ^[Oq) may be different on your system. You can press Ctrl+v followed by the key in question to get the code for your terminal.


To complete the answer that @adaephon gave, the following are the correct bindings for the Apple USB keyboard (A1243). These need to be added to ~/.zshrc.

# Fix numeric keypad
# 0 . Enter
bindkey -s "^[Op" "0"
bindkey -s "^[On" "."
bindkey -s "^[OM" "^M"
# 1 2 3
bindkey -s "^[Oq" "1"
bindkey -s "^[Or" "2"
bindkey -s "^[Os" "3"
# 4 5 6
bindkey -s "^[Ot" "4"
bindkey -s "^[Ou" "5"
bindkey -s "^[Ov" "6"
# 7 8 9
bindkey -s "^[Ow" "7"
bindkey -s "^[Ox" "8"
bindkey -s "^[Oy" "9"
# + -  * / =
bindkey -s "^[Ol" "+"
bindkey -s "^[Om" "-"
bindkey -s "^[Oj" "*"
bindkey -s "^[Oo" "/"
bindkey -s "^[OX" "="

Also complementing answer from @adaephon. Here is what I had to use for my Dell Desktop, might be of help. Just add it in your .zshrc file.

(BTW dont add bindings from all answers, you should just have one binding per key)

# Keypad
# 0 . Enter
bindkey -s "^[Op" "0"
bindkey -s "^[On" "."
bindkey -s "^[OM" "^M"
# 1 2 3
bindkey -s "^[Oq" "1"
bindkey -s "^[Or" "2"
bindkey -s "^[Os" "3"
# 4 5 6
bindkey -s "^[Ot" "4"
bindkey -s "^[Ou" "5"
bindkey -s "^[Ov" "6"
# 7 8 9
bindkey -s "^[Ow" "7"
bindkey -s "^[Ox" "8"
bindkey -s "^[Oy" "9"
# + -  * /
bindkey -s "^[OQ" "/"
bindkey -s "^[OR" "*"
bindkey -s "^[OS" "-"
bindkey -s "^[Ol" "+"
#END Keypad

Tags:

Zsh