Interpret zsh bindkey escaped sequences

ejel picture ejel · Mar 15, 2011 · Viewed 7.8k times · Source

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.

Answer

horsh picture horsh · Mar 15, 2011

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