3

I'm learning zsh at the moment, and configuring my ~/.zshrc. On the Archwiki, I found a snippet to change the history searching behavior:

autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search

[[ -n "$key[Up]"   ]] && bindkey -- "$key[Up]"   up-line-or-beginning-search
[[ -n "$key[Down]" ]] && bindkey -- "$key[Down]" down-line-or-beginning-search

After scouring the man pages however, I cannot find any documentation on the key keyword or what parameters it can take. grep-ing for man zshall | grep -i "key.*\[" does not show any more information, nor does the man zshzle entry for bindkey. The closest relevant match is the following from man zshcontrib:

...
source ${ZDOTDIR:-$HOME}/.zkbd/$TERM-$VENDOR-$OSTYPE
[[ -n ${key[Left]} ]] && bindkey "${key[Left]}" backward-char
[[ -n ${key[Right]} ]] && bindkey "${key[Right]}" forward-char
# etc.
...

I found more examples at zshwiki and an answer from @rayandrews on Unix.SE, although neither appear to be or claim to be the full list of parameters to key, nor describe where/how key is defined.

Where can I learn more about key[...] and how to use it?

8
  • The wiki shows an example, which is not in the zsh sources (though some packager may have added that). For another example, see zkbd. Jul 15, 2017 at 22:40
  • Yes the wiki is not official documentation. I did already try running zkbd, which created an xterm-256color-:0 file for me. It doesn't contain any more info than I already had unfortunately, but I can add the contents to my post if you think it's helpful.
    – nivk
    Jul 15, 2017 at 22:49
  • The wiki example shows how to assign terminfo capabilities to the key hash. zsh has a list of the standard terminfo key-names (such as kdch1) and the example uses a more understandable Delete. In regard to your question, the parameter to key is the user-assigned name. Someone could define additional names, but there's no "full list". Jul 15, 2017 at 22:56
  • It's possible that I may need to open another question to understand how this works. I can see that key[...] is just a dictionary for the more opaque terminfo parameters. But I have not defined this dictionary anywhere, so (in the Archwiki example) how does $key[Up] not throw an error? The key dictionary must exist somewhere on my machine, no?
    – nivk
    Jul 15, 2017 at 23:11
  • A packager may have added it to the zsh initialization-scripts (different systems have different packages...). Jul 15, 2017 at 23:31

1 Answer 1

2

On Debian, /etc/zsh/zshrc has:

    typeset -A key
    key=(
        BackSpace  "${terminfo[kbs]}"
        Home       "${terminfo[khome]}"
        End        "${terminfo[kend]}"
        Insert     "${terminfo[kich1]}"
        Delete     "${terminfo[kdch1]}"
        Up         "${terminfo[kcuu1]}"
        Down       "${terminfo[kcud1]}"
        Left       "${terminfo[kcub1]}"
        Right      "${terminfo[kcuf1]}"
        PageUp     "${terminfo[kpp]}"
        PageDown   "${terminfo[knp]}"
    )

It was added by that commit in zsh Debian package 5.0.0-1.

Possibly Arch copied that zshrc from Debian. In any case, that is not part of the upstream zsh distribution.

You can always look at the definition of the $key hash with:

typeset -p key | sed -n l

Or

printf '%q => %q\n' "${(@kv)key}"
3
  • This is exactly what I was looking for. Thank you!
    – nivk
    Jul 23, 2017 at 19:14
  • Can you give an example of what to substitute for key in typeset -p key | sed -n l, please? Everything I've tried returns the error "no such variable".
    – ian
    Sep 22, 2017 at 18:07
  • 1
    @iain, the question was where is the $key hash documented. The answer is it's not be cause $key is just a variable defined in the system's zshrc by some Linux distributions. Yours is not one of them, $key is not defined, it you want to bind some function keys to some actions, you can use the $terminfo hash directly or copy the definition of that $key hash into your .zshrc Sep 23, 2017 at 6:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .