开发者

Interpret zsh bindkey escaped sequences

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 determ开发者_运维问答ine the keys regardless of the physical keyboard.


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


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.

Interpret zsh bindkey escaped sequences

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜