在期望脚本中发送 INSERT 和 F12

Send INSERT and F12 in expect script

我知道为了在 expect 脚本中发送 return 我会这样做:

send -- "\r"

INSERTF12 键的发送命令是什么?我在网上查过了,到处都找不到。

使用 infocmp xterm 作为来源:

$ infocmp xterm |grep -oP "f12=\S+|kich1=\S+"
f12=\E[24~,
kich1=\E[2~,
  • 插入:send -- "[2~"send -- {[2~}
  • F12: send -- "[24~"send -- {[24~}

识别当您按 INSERT ,然后按 F12 按 运行 od -c 时终端发送的字符:

$ od -c
^[[2z^[[193z       <-- Press keys here, then ENTER and Ctrl-D
0000000 033   [   2   z 033   [   1   9   3   z  \n
0000013

这意味着你应该send -- "\E\[2z\E\[193z"。请注意,这是特定于终端的。上面的序列是我的TERM=xterm-256color.

发来的

我不得不说最初接受的answer是不正确的,因为

  1. 不同的终端类型,真实的字符序列是不一样的;
  2. send -- "[2~" 是错误的,因为
    1. [ 在 Tcl 中有特殊含义(命令替换)所以它应该被反斜杠转义;
    2. 缺少 ESC 字符(\E,如 infocmp 的输出);

正确的方法:

set kf12 [exec tput kf12]
set kins [exec tput kich1]
... ...
send $kf12

如果需要手动指定TERM类型,使用tput -T:

  • -Ttype

    indicates the type of terminal. Normally this option is unnecessary, because the default is taken from the environment variable TERM. If -T is specified, then the shell variables LINES and COLUMNS will also be ignored.

对于魔术字符串 kf12kich1,请在 terminfo manual 页面中搜索。