Zsh get terminfo keys with modifiers

The Arch Linux Wiki page on Zsh has an example that gets key sequences from terminfo in it.

typeset -g -A key


But how would I get ones with modifiers? Specifically Alt+j and Alt+k.

Thank you.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The ncurses terminal database defines these (see How can I use shift- or control-modifiers? in the ncurses FAQ).

Most of this it’s based on work done for xterm (see this page, for example).

Those are unique names (which zsh ignores, since they’re not in its dictionary of predefined terminal names — see Src/Modules/terminfo.c). You’d have to make some shell assignment, say, to the output of tput to get the data into a variable.

Alt is typically not considered a modifier when applied to an ordinary key such as j or k in the xterm.

It has a mode (modifyOtherKeys), which can tell it to send an escape sequence for different combinations (see How can my program distinguish control-I from tab? in the xterm FAQ).

In this case, zsh provides no help at all. Likewise, the terminal will not offer a table of the results (it deals with special keys). A customized application would be needed.

You may have in mind the meta feature. That does have a terminfo feature (smm to enable, rmm to disable). However, most terminals (other than xterm) implement this differently from the documented behavior. In terminfo(5):

If the terminal has a “meta key” which acts as a shift key, setting the
8th bit of any character transmitted, this fact can be indicated with
km. Otherwise, software will assume that the 8th bit is parity and it
will usually be cleared. If strings exist to turn this “meta mode” on
and off, they can be given as smm and rmm.

That’s partly due to the backwards definition found in bash documentation (see my answer for details). But in any case, terminfo will not hold the information that tells your script what characters might be prefixed with Escape.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply