How do I get the F1-F12 keys to switch screens in gnu screen in cygwin when connecting via SSH?

Mikey picture Mikey · Jan 11, 2011 · Viewed 12k times · Source

I'm connecting to a desktop running cygwin via SSH from the terminal app in Mac OS X. I have already started screen on the cygwin side and can connect to it over the SSH session. Furthermore, I have the following in the .screenrc file:

bindkey -k k1 select 1  #  F1 = screen 1
bindkey -k k2 select 2  #  F2 = screen 2
bindkey -k k3 select 3  #  F3 = screen 3
bindkey -k k4 select 4  #  F4 = screen 4
bindkey -k k5 select 5  #  F5 = screen 5
bindkey -k k6 select 6  #  F6 = screen 6
bindkey -k k7 select 7  #  F7 = screen 7
bindkey -k k8 select 8  #  F8 = screen 8
bindkey -k k9 select 9  #  F9 = screen 9
bindkey -k F1 prev      # F11 = prev
bindkey -k F2 next      # F12 = next

However, when I start multiple windows in screen and attempt to switch between them via the function keys, all I get is a beep.

I have tried various settings for $TERM (e.g. ansi, cygwin, xterm-color, vt100) and they don't really seem to affect anything.

I have verified that the terminal app is in fact sending the escape sequence for the function key that I'm expecting and that my bash shell (running inside screen) is receiving it. For example, for F1, it sends the following (hexdump is a perl script I wrote that takes STDIN in binmode and outputs it as a hexadecimal/ascii dump):

% hexdump
[press F1 and then hit ^D to terminate input]
00000000:  1b4f50                               .OP

If things were working correctly, I don't think bash should receive the escape sequence because screen should have caught it and turned it into a command.

How do I get the function keys to work?

Answer

Mr Fooz picture Mr Fooz · Jun 11, 2012

If you have a more bizarre setup (e.g. Windows -> PuTTY -> Linux) where the standard bindkey -k solution doesn't quite work right, you can use the showkey command:

showkey -a

to find the mapping from keystrokes to key codes. In my particular case, putting these in ~/.screenrc did the trick:

bindkey "^[[11~" select 1
bindkey "^[[12~" select 2
bindkey "^[[13~" select 3
bindkey "^[[14~" select 4
bindkey "^[[15~" select 5
bindkey "^[[17~" select 6
bindkey "^[[18~" select 7
bindkey "^[[19~" select 8
bindkey "^[[20~" select 9
bindkey "^[[21~" select 10
bindkey "^[[23~" select 11
bindkey "^[[24~" select 12