解释 zsh bindkey 转义序列
我通常会在网络上找到有趣的 zsh 键绑定设置(通过 bindkey
命令)。我的问题是如何解释这些转义序列映射到的内容?例如,以下是 oh-my-zsh 的 key-bindings.zsh
bindkey "^[[H" beginning-of-line
bindkey "^[[1~" beginning-of-line
bindkey "^[[F" end-of-line
bindkey "^[[4~" end-of-line
有关于如何进行的参考吗这些键盘映射代表什么?另外,它是特定于 zsh 的还是特定于平台的?
我知道我可以使用 cat
或 Ctrl -V 查找某些键对应的转义序列。鉴于我可以暴力地找到反向匹配,但这对于键盘上不存在的键(例如 Mac 笔记本电脑上的 Home/End)不起作用。因此,我更喜欢能够确定按键而不管物理键盘如何的方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果谈到典型的 unix/linux 事件流程,图片大致如下。
终端仿真器程序接收 X 事件,例如按下某某按钮,释放另一个按钮。例如,可以使用 xev 实用程序来跟踪这些事件。然后,终端模拟器将这些事件转换为转义序列。
这个翻译并不是一成不变的。可以配置。不同的终端仿真器配置不同。例如,xterm 翻译可以在 .Xdefaults 中设置,如下所示:
注意 0x1B 是 ESC。 ESC 也打印为 ^[。
现在,zsh 使用 zle (并且 bash 使用 readline 库用于相同目的)
它解释一些序列以在输入行周围移动并执行编辑操作。
以下文本应提供更多附加细节。
Zsh Line 编辑器说明
有关转义序列的维基百科文章
和
Xterm 控制序列
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:
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
我的答案是针对 2021 年使用带有默认 zsh Termnial 的 MacOSX 的现代读者:
My answer is for modern readers in 2021 using MacOSX with default zsh Termnial: