bash 如何找出我的终端需要发送的转义序列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19062315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find out what escape sequence my terminal needs to send?
提问by user1757006
I am using the terminal application on osX to connect to ubuntu (12.04 i think). I have the terminal at it's default setting of xerm-256. In this mode the terminal sends the sequence of \033OQ when F2 is pressed. There is no mapping however for Shift-F2 or Control-F2. There is also no sequences set for Shift-Left, Shift-Right, Control-Left, Control-Right, etc. I need some of these keys to operate features from time to time in various programs. Take for example byobu. Shift-F2 and Control-F2 are used to split the screen vertically and horizontally. In tmux the arrow keys are used with shift and control for resizing panes.
我在 osX 上使用终端应用程序连接到 ubuntu(我认为是 12.04)。我的终端默认设置为 xerm-256。在这种模式下,当按下 F2 时,终端发送 \033OQ 的序列。但是 Shift-F2 或 Control-F2 没有映射。也没有为 Shift-Left、Shift-Right、Control-Left、Control-Right 等设置序列。我需要其中一些键来不时地在各种程序中操作功能。以byobu为例。Shift-F2 和 Control-F2 用于垂直和水平拆分屏幕。在 tmux 中,箭头键与 shift 和 control 一起用于调整窗格大小。
My problem is that I don't know what sequence the terminal should send. How do I find out what these sequences should be? I know that I can press Control-v and then a key to get the code that is being received but this is not what I need. I don't need to see what sequence is being sent. I need to know what sequence I need to send.
我的问题是我不知道终端应该发送什么序列。我如何找出这些序列应该是什么?我知道我可以按 Control-v 然后按一个键来获取正在接收的代码,但这不是我需要的。我不需要查看正在发送的序列。我需要知道我需要发送什么序列。
For instance my left arrow sends ^[OD. My Control-Left sends ^[[5D]. However my up and down keys have no distinction. My down sends ^[OB and Control-down also sends ^[OB. Same problem with my up key. It is the same with or without shift.
例如我的左箭头发送 ^[OD. 我的 Control-Left 发送 ^[[5D]。但是我的上下键没有区别。我的向下发送 ^[OB,Control-down 也发送 ^[OB。我的向上键也有同样的问题。无论有无移位都是一样的。
Is there a list somewhere with all of the keys and there codes, specifically with modifiers such as shift, control, alt/option?
是否有包含所有键和代码的列表,特别是带有诸如 shift、control、alt/option 等修饰符的列表?
How can I find out what the expected sequence is so I can send what is needed?
我怎样才能找出预期的顺序,以便我可以发送所需的内容?
回答by Chris Johnsen
XTerm
XTerm
Since you are advertising that you are using an xterm(via your TERM value), you will most likely want to arrange to send the sequences that xtermwould send for these keys. The ctlseqsdocumentation from xtermdescribes these particular modified key sequences at the bottom of the PC-Style Function Keyssection:
由于您正在宣传您正在使用xterm(通过您的 TERM 值),您很可能希望安排发送xterm将为这些密钥发送的序列。xterm的ctlseqs文档在PC-Style Function Keys部分的底部描述了这些经过特殊修改的键序列:
… xterm recognizes function key modifiers which are parameters appended before the final character of the control sequence.
2 Shift 3 Alt 4 Shift + Alt 5 Control 6 Control + Shift ?
... xterm 识别功能键修饰符,它们是附加在控制序列的最后一个字符之前的参数。
2 Shift 3 Alt 4 Shift + Alt 5 Control 6 Control + Shift ?
Examples
例子
F5sends ^[[15~
, so Shift-F5should send ^[[15;2~
(i.e. add ;2
before the final character)
F5发送^[[15~
,所以Shift-F5应该发送^[[15;2~
(即;2
在最后一个字符之前添加)
The arrow keys and the first four function keys are a bit different. They often use SS3-based sequences (starting with ^[O
); these will need to be changed to CSI-based equivalents (starting with ^[[
) since SS3 sequences can not have parameters. Also, the normal sequences for the keys do not usually have a numeric parameter, so a placeholder(?) 1
parameter is also added:
方向键和前四个功能键有点不同。他们经常使用基于 SS3 的序列(以 开头^[O
);这些将需要更改为基于 CSI 的等效项(以 开头^[[
),因为 SS3 序列不能有参数。此外,键的正常序列通常没有数字参数,因此1
还添加了占位符(?)参数:
Upsends ^[[A
or ^[OA
, so Shift+Upshould send ^[[1;2A
(i.e. switch to CSI, and add 1;2
before the final character)
Up发送^[[A
or ^[OA
,所以Shift+Up应该发送^[[1;2A
(即切换到 CSI,并1;2
在最后一个字符之前添加)
F1sends ^[OP
, so Shift+F1should send ^[[1;2P
(i.e. switch to CSI, add 1;2
before the final character)
F1发送^[OP
,所以Shift+F1应该发送^[[1;2P
(即切换到 CSI,1;2
在最后一个字符之前添加)
You might also like to look at the source code of various terminal emulators to see whay they do. For example, the relevant bits of tmuxare in its xterm-keys.c
file.
您可能还想查看各种终端仿真器的源代码以了解它们的作用。例如,tmux的相关位在其xterm-keys.c
文件中。
Configuration
配置
Since your terminal emulator is not already sending all the sequences you want to support, you will have to configure it to do so. The built-in terminal emulator that comes with OS X, Terminal, has a few keys preconfigured, but you will probably have to add most of them yourself. You can get to the list of keys by invoking the Terminal> Preferences…menu item, selecting the top-level Settingstab, picking the appropriate profile, and switching to its Keyboardtab. From there you can add and remove key definitions. For example, you could define F2
with shift
to send string to shell:
(sic) and type ESCfollowed by [1;2Q
into the string text box (the ESCwill show up as \033
, this is okay).
由于您的终端仿真器尚未发送您想要支持的所有序列,因此您必须对其进行配置才能这样做。OS X 附带的内置终端模拟器Terminal有一些预配置的键,但您可能需要自己添加其中的大部分。您可以通过调用终端>首选项...菜单项,选择顶级设置选项卡,选择适当的配置文件并切换到其键盘选项卡来访问键列表。从那里您可以添加和删除密钥定义。例如,您可以F2
使用shift
to send string to shell:
(sic)定义并在字符串文本框中键入ESC后跟[1;2Q
(ESC将显示为\033
,这没关系)。
Note: Changing the definition of Option-Rightand Option-Leftmight affect how some programs (e.g. shells and Emacs) work with those keystrokes. The default configuration sends ESC-f and ESC-b, which are Emacs-style keystrokes for backward-word and forward-word that many programs will understand by default. This configuration seems backwards to me; the shell (and other programs) should be configured to recognize proper modified arrow key sequences instead.
注意:更改Option-Right和Option-的定义Left可能会影响某些程序(例如 shell 和 Emacs)使用这些键击的方式。默认配置发送 ESC-f 和 ESC-b,它们是 Emacs 风格的后向字和前向字击键,许多程序默认都能理解。这种配置在我看来是倒退的;外壳(和其他程序)应该配置为识别正确修改的箭头键序列。
You might also want to try a different terminal emulator. iTerm2has built-in support for most of these modified keys (maybe not the Control+Fn variants), so there would be less to manually configure.
您可能还想尝试不同的终端模拟器。iTerm2内置支持大多数这些修改后的键(可能不是 Control+Fn 变体),因此手动配置的次数会更少。
Also, note that OS X has some system-wide shortcuts defined for some of the Control-Fxcombinations (see System Preferences, Keyboard, Keyboard Shortcuts, Keyboard & Text Input). You will have to disable some of these shortcuts to make sure that Terminalor iTermhas a chance to “see” your desired key combinations.
另请注意,OS X 为某些Control-Fx组合定义了一些系统范围的快捷键(请参阅系统偏好设置、键盘、键盘快捷键、键盘和文本输入)。您将不得不禁用其中一些快捷方式,以确保终端或iTerm有机会“看到”您想要的组合键。