Send INSERT and F12 in expect script Send INSERT and F12 in expect script unix unix

Send INSERT and F12 in expect script


Using infocmp xterm as source:

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


I have to say the initially accepted answer is not correct because

  1. The real char sequence is not the same for different terminal types;
  2. send -- "[2~" is wrong because
    1. [ in Tcl has special meaning (command substitution) so it should be backslash-escaped;
    2. The ESC char (\E as in infocmp's output) is missing;

The correct way:

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

If you need to manually specify the TERM type, use 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.

For the magic strings kf12 and kich1, search in the terminfo manual page.


Identify the characters that your terminal sends when you press INSERT followed by F12 by running od -c:

$ od -c^[[2z^[[193z       <-- Press keys here, then ENTER and Ctrl-D0000000 033   [   2   z 033   [   1   9   3   z  \n0000013

This means you should send -- "\E\[2z\E\[193z". Note that this is terminal specific. The sequence above is sent by my TERM=xterm-256color.