2

I have zsh shell in a Linux server, and connect to the server from screen sessions in different computers. I'm trying to get control keys, such as home and end, to function correctly.

Because zsh doesn't use the GNU Readline library, I need to take care of mapping the terminal sequences to zsh commands. First I use zkbd to find out the sequences sent by each key stroke, and then I map them to the correct commands. This is what I have in .zshrc:

autoload zkbd
[ ! -f "${ZDOTDIR:-$HOME}/.zkbd/$TERM-${DISPLAY:-$VENDOR-$OSTYPE}" ] && zkbd
source "${ZDOTDIR:-$HOME}/.zkbd/$TERM-${DISPLAY:-$VENDOR-$OSTYPE}"

[ -n "${key[Backspace]}" ] && bindkey "${key[Backspace]}" backward-delete-char
[ -n "${key[Home]}" ] && bindkey "${key[Home]}" beginning-of-line
...

Now I have two problems:

  1. Usually when I connect to the server, the sequence number in $DISPLAY environment variable gets a new value. Consequently, zkbd gets run, and I have to go through pressing all the control keys. Is it necessary to have the key sequences dependent on $DISPLAY?
  2. When I connect to the server from a screen session, $TERM environment variable will be set to screen. Still, depending on which computer I'm connecting from, the sequences of some keys differ (e.g. F1 is either ^[[11~ or ^[OP). How should I name the zkbd files in order to distinguish between the different mappings?
2

Rather than simply use TERM=screen, the screen program has a feature which you could use to set different values of TERM. This assumes that you've installed a complete ncurses terminal database, and use a TERM outside screen that corresponds to the actual terminal.

For a given TERM, if there is a corresponding "screen.$TERM" entry in the terminal database, then screen will use that. The ncurses terminal database has several of these, to match the actual behavior of terminals that set TERM=xterm, e.g., screen.Eterm, screen.gnome, screen.konsole, screen.konsole-256color, screen.linux, screen.mlterm, screen.mlterm-256color, screen.mrxvt, screen.putty, screen.putty-256color, screen.rxvt, screen.teraterm, screen.vte, screen.vte-256color, screen.xterm-256color, screen.xterm-new, screen.xterm-r6, screen.xterm-xfree86.

Once you've setup things to use the terminal database, there's no need for special/magic configuration files of your own. Besides, how would you tell the server which file to use? The real information is on your client, where you know which terminal emulator you are actually using.

zsh lets you use this information directly with the $terminfo array.

Further reading:

  • The server has ncurses installed. I'm using different clients, e.g. PuTTY, which doesn't use ncurses, but I can set the terminal-type string (currently xterm). The server doesn't have screen.xterm entry in the terminal database, but it does have e.g. screen.xterm-new and putty. Should I set the terminal-type string to xterm-new or putty? – Seppo Enarvi Aug 15 '16 at 9:52
  • "putty" should work better (use "infocmp putty xterm-new" to see differences). ncurses doesn't provide "screen.xterm" because it interferes with users without a complete terminal database. – Thomas Dickey Aug 15 '16 at 20:45
  • So how can I set the keyboard bindings on then server then? For bindkey I need to know the sequences sent by backspace, home, page up, etc, but these are all different depending on where I'm connecting from. If I'm starting a session from Linux console, TERM=linux (inside screen TERM=screen.linux) and e.g. home key sends ^[[1~. If I'm starting a session from Gnome terminal, TERM=xtermand home sends ^[OH. I have ncurses installed. – Seppo Enarvi Aug 16 '16 at 13:55
  • In your question, you're using bindkey with the values returned from the terminfo database, e.g., "${key[Backspace]}". If the terminal database entries work, then there's no need to use explicit escape sequences. – Thomas Dickey Aug 16 '16 at 13:57
  • In my question I'm using values generated using zkbd and saved in a text file. If I understood what you mean, I shouldn't use zkbd, but read the values from the ncurses terminal database. Could you still tell me how to read the values from the correct file? – Seppo Enarvi Aug 16 '16 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.