18

I usually find interesting zsh keybinding settings (through bindkey command) around the web. My question is how do I interpret what these escaped sequences mapped to? For instance, here is a snippet from oh-my-zsh's key-bindings.zsh

bindkey "^[[H" beginning-of-line
bindkey "^[[1~" beginning-of-line
bindkey "^[[F"  end-of-line
bindkey "^[[4~" end-of-line

Is there a reference on how do these keymaps represented? Also, is it zsh-specific or platform specific at all?

I am aware that I can use either cat or Ctrl-V to find the corresponding escaped sequence for certain keys. Given that I could brute force to find the reverse match, but this would not work for the keys that do not exist on my keyboard (e.g. Home/End on Mac laptops). Thus, I'd prefer methods that could determine the keys regardless of the physical keyboard.

Community
  • 1
  • 1
ejel
  • 4,135
  • 9
  • 32
  • 39

2 Answers2

6

If speaking of a typical unix/linux flow of events the picture is roughly the following.

The terminal emulator program recieves the X events such as so and so button pressed, another button is released. Those events can be tracked with xev utility, for example. The terminal emulator then translates those events into escape sequences.

This translation is not set in stone. It can be configured. Different terminal emulators are configured differently. For example xterm translation can be set up in .Xdefaults like that:

XTerm*VT100*Translations:#override \
Ctrl<Key>Left:          string(0x1B) string(OD) \n\
Ctrl<Key>Right:          string(0x1B) string(OC) \n\

Note 0x1B which is ESC. ESC is also printed as ^[.

Now, zsh uses zle (and bash uses readline library for the same purpose) which interprets some of the sequences to move around the input line and perform editing actions.

The following texts should provide more additional details.

Zsh Line editor description

Wikipedia article on escape sequences

and

Xterm Control Sequences

Ruud
  • 3,118
  • 3
  • 39
  • 51
Alexander Gorshenev
  • 2,769
  • 18
  • 33
  • But that doesn't include other terminal emulators which may not be running under X11. – Dennis Williamson Mar 15 '11 at 17:10
  • Right. This is many to many situations. Many teminals, many shells, many keyboard drivers etc. All of them are being setup differently. I never could find where to setup the translations in gnome terminal, for example. – Alexander Gorshenev Mar 15 '11 at 18:31
  • Thank you for the explanation. Please excuse my ignorant though, as I'm not sure if it answers my question. I looked up the given references and still couldn't find how to translate escaped sequences to corresponding key sequences (i.e. which key presses mapped to "^[[H") – ejel Mar 16 '11 at 17:38
  • 1
    That depends on the particular terminal emulator you have. WHich one you have? – Alexander Gorshenev Mar 16 '11 at 17:49
  • It's OS X's Terminal declared as xterm-color. – ejel Mar 16 '11 at 18:00
  • Then what you need is essentially the "VT100" section and "PC-style function keys" section of the above mentioned Xterm Control Sequences document. I don't think there is a way to ask xterm to dump just a table you are thinking about. All this machinery is much older than PC keyboard itself, so what is natural for us today is not natural for xterm. – Alexander Gorshenev Mar 16 '11 at 18:11
  • I'd appreciate if you could show me how to interpret the document *grin* using the sample keymaps in my original question. I have quite a hard time comprehending that document. – ejel Mar 16 '11 at 22:36
  • 1
    "PC-Style Function Keys" has the second table which says "Home is CSI H, End is CSI F". CSI happens to be described in the very beginning as "ESC [ Control Sequence Introducer ( CSI is 0x9b)". So this is basicly two of four of your initial example. "^[[H" == "ESC[H" = "CSI H". So xterm translates Home keypress to ^[[H which zsh interprets as beginning-of-line. "CSI 1 ~" and "CSI 4 ~" are described as Home and End of 6-key keypad of VT220. To get the full list of zsh bindings see "man zshzle" and type "bindkey -M emacs". – Alexander Gorshenev Mar 16 '11 at 23:10
  • 5
    Why cant someone just create a list of different key mappings, start of with common or that persons preferred shell, and then others can contribute. I still dont understand how to reverse lookup what I currently have! eg `\e[3~`, `^[OH` etc etc! – Ian Vaughan May 20 '12 at 12:44
-2

My answer is for modern readers in 2021 using MacOSX with default zsh Termnial:

  1. Run your Terminal, press + , to open Preferences.
  2. Select Profiles > Keyboard tab, then here you are, there are all your mappings.

Preferences of Terminal, find Profiles, Keyboard then there are the mappings

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
  • zsh is not as terminal. zsh is a shell. What you are showing is probably iterm, one of many terminal emulators. – undg Feb 07 '23 at 22:42
  • @undg: By "zsh Terminal" I meant it's a "Terminal" (capitalize the first letter because this is the **name** of macOS's built-in terminal. It's NOT an emulator) and "zsh" is the adjective to describe this noun that it's using zsh as a default shell. Any question? – NeoZoom.lua Feb 08 '23 at 01:11