Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get control characters for Ctrl+Left from terminfo in Zsh

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.

like image 414
artfulrobot Avatar asked Oct 19 '22 07:10

artfulrobot


1 Answers

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"
like image 170
Thomas Dickey Avatar answered Oct 29 '22 04:10

Thomas Dickey