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?

nivk
  • 165
  • 6
  • The wiki shows an *example*, which is not in the zsh sources (though some packager may have added that). For another example, see [zkbd](https://github.com/zsh-users/zsh/blob/master/Functions/Misc/zkbd). – Thomas Dickey Jul 15 '17 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 '17 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". – Thomas Dickey Jul 15 '17 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 '17 at 23:11
  • A packager may have added it to the zsh initialization-scripts (different systems have different packages...). – Thomas Dickey Jul 15 '17 at 23:31
  • I see. So I'd need to dig into the installer to figure it out? Yikes... – nivk Jul 15 '17 at 23:32
  • Probably (`pacman` should have something to list the contents of a package, and from that you could grep for things that would be in the hash, to see where `key` is populated). – Thomas Dickey Jul 15 '17 at 23:34
  • Look at m recent answer, it may shed some light: https://unix.stackexchange.com/a/379084/80886 – jimmij Jul 20 '17 at 20:50

1 Answers1

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}"
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • This is exactly what I was looking for. Thank you! – nivk Jul 23 '17 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 '17 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 – Stéphane Chazelas Sep 23 '17 at 06:38