5

I'm using terminfo to find out what they control sequence is for the Left (Right, ...) key in my terminal, so that I can then map it to something useful (move left) in my zshrc like this:

typeset -A key
left=${terminfo[kcub1]}
bindkey "$left" backward-char

(there's some other things to get this to work, see source). This is helpful because I use several terminal emulators and these control sequences change. e.g. I use Gnome Terminal on Ubuntu to ssh to a Debian box and then to use tmux inside that...

But how can I find out the key codes for the CTRL+LEFT (and RIGHT...) from terminfo? I'd like to map these to word left/right.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
artfulrobot
  • 20,637
  • 11
  • 55
  • 81

1 Answers1

3

The controlleft-arrow, etc., are not standard terminfo capabilities. ncurses provides these as extensions (see discussion in terminal database), and applications that know how to use the extensions can fetch them.

zsh's source code hints that it will use the terminfo library's tables, e.g., for strnames if available, but ncurses implements the extended names in a separate area of memory. (The terminfo data dates to around 2005; the extension itself dates to ncurses 5.0 in 1999).

However, the implementation is (as of 2018) incomplete since zsh does not use the extended information to find the names. The example given in ZSH for loop array variable issue, e.g.,

for key val in ${(kv)terminfo}; do
    echo "$key -> $val"
done

gives only the predefined names.

zsh uses tigetstr to retrieve string-capabilities. If you happen to know the name (or see it in infocmp's -x listing) you can use that as an index for its terminfo[] array.

The extended names are based on xterm's code for modifiers, which is listed in the comment above the xterm+pcfkeys entry in the terminal database:

  • controlleft-arrow is "kLFT5"
  • controlright-arrow is "kRIT5"
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • It looks like zsh now has the ncurses extensions! This answer could be updated to reflect that. – cbarrick Aug 30 '18 at 16:42
  • Its source-code hasn't been modified to address any of the details mentioned. You may be thinking of some add-on script. – Thomas Dickey Aug 30 '18 at 20:21
  • 1
    Hmm. I haven't checked the source code, but I _can_ get the ncurses extended capabilities from $terminfo: `echo $terminfo[kUP5] | od -a`. – cbarrick Aug 30 '18 at 21:07