How to use Ctrl-Tab in GNU Screen

GNU Screen allows you to open several sub-windows within one terminal window. By default, you switch between them using Ctrl-A followed by n or p. I think this is a bit clumsy, I would like to switch with Ctrl-Tab and Ctrl-Shift-Tab just like you switch tabs in Firefox and many other applications. The sub-windows in Screen is conceptually just like tabs in Firefox, so it’s logical to use the same keys to switch between them.

Screen can be configured to use any key combination for switching sub-window by using the bindkey command. However, Screen can only recognize the key combinations that your terminal emulator actually intercept and send a unique code for. By default, most terminal emulators do not intercept Ctrl-Tab, they just send the same code as for Tab. And you certainly not want to use that since Tab is used for tab completion in the shell.

So you need to configure your terminal emulator to intercept and send a unique code for Ctrl-Tab. In xterm, you can do that by setting the X resource XTerm.vt100.modifyOtherKeys: 2. Now xterm sends ^[[27;5;9~ for Ctrl-Tab and ^[[27;6;9~ for Ctrl-Shift-Tab (^[ is ESC). However, you don’t want to use this since it mess up other things. You need to configure just Ctrl-Tab and Ctrl-Shift-Tab without altering any other keys. This can be done using the translation feature. Add this to your ~/.Xresources file (you need to log out for this to take effect):

*vt100.translations: #override \n\
        Ctrl ~Shift <Key>Tab: string(0x1b) string("[27;5;9~") \n \
        Ctrl Shift <Key>Tab: string(0x1b) string("[27;6;9~") \n

Then add this to your ~/.screenrc file:

# Ctrl-Tab
bindkey "^[[27;5;9~" next

# Ctrl-Shift-Tab
bindkey "^[[27;6;9~" prev

This works in xterm. I’m not sure if it works in other terminal emulators.

This entry was posted in Linux. Bookmark the permalink.

9 Responses to How to use Ctrl-Tab in GNU Screen

  1. Pingback: Using Ctrl+Tab in GNU Screen over PuTTY at scnr.net

  2. Pingback: ターミナルでCtrl+Tabとかを使う | SanRin舎

  3. Andre Klärner says:

    if someone would like to have it for urxvt:

    ! Switching screen-windows
    urxvt*keysym.C-Tab: 33[27;5;9~
    urxvt*keysym.C-S-Tab: 33[27;6;9~

    but as Control-Shift-Tab collides with other urxvt-defaults (the enter-unicode mode) I rather stick to my version: Shift-Left and Shift-Right

    ! Switching screen-windows
    urxvt*keysym.S-Right: 33[27;5;9~
    urxvt*keysym.S-Left: 33[27;6;9~

    .screenrc needs also what is above.

  4. Dan Jones says:

    I just found this post, while I was looking to do something very similar.

    In my browser, mouse buttons 8 and 9 map to back (in page history) and forward, respectively. I thought it might be nice for the same buttons to switch between screens.

    So, I tried to combine the information from this post, and your previous post about using the mousewheel in screen, and came up with this:

    In .Xresources:
    *vt100.translations: #override \n\
    : string(0x1b) string(“[27;5;9~”) \n \
    : string(0x1b) string(“[27;6;9~”) \n

    In .screenrc:
    # Button 9
    bindkey “^[[27;5;9~” next

    # Button 8
    bindkey “^[[27;6;9~” prev

    Unfortunately, I got nothing. I know the buttons are recognized in X. They work in browser (both Firefox, and Chrome), as I said, and in xev, the buttons produce the following:
    ButtonPress event, serial 36, synthetic NO, window 0x2e00001,
    root 0xc8, subw 0x0, time 1492063874, (73,131), root:(455,154),
    state 0x10, button 8, same_screen YES

    ButtonPress event, serial 36, synthetic NO, window 0x2e00001,
    root 0xc8, subw 0x0, time 1492065330, (73,131), root:(455,154),
    state 0x10, button 9, same_screen YES

    So, how might I go about debugging this? Do you have any ideas?

  5. Dan Jones says:

    My last comment got mangled, because it thought I was putting in HTML tags.
    The .Xresources should have read (without those extra spaces):
    *vt100.translations: #override \n\
    : string(0x1b) string(“[27;5;9~”) \n \
    : string(0x1b) string(“[27;6;9~”) \n

  6. Dan Jones says:

    Sorry about that, let’s try again:
    *vt100.translations: #override \n\
    «Btn9Down»: string(0x1b) string(“[27;5;9~”) \n \
    «Btn8Down»: string(0x1b) string(“[27;6;9~”) \n

    But with proper angle brackets.

  7. andy says:

    Quoting…
    “GNU Screen allows you to open several sub-windows within one terminal window. By default, you switch between them using Ctrl-A followed by n or p”

    Not here. I MUST use Ctrl-A followed by TAB.
    Ctrl-A n / Ctrl-A p will do something _completely_ different: it will clone a terminal output. That is (try it out!), if you do that with xterm, your input in bash #0 will be *duplicated* in bash #1 ! You’d hardly ever want this, but rather want to use the split panes independently.

  8. Pingback: Unix:GNU screen: move between regions – Unix Questions

  9. Julien says:

    I got this working under Babun/Cygwin as well. The terminal is mintty, which normally responds to Ctrl+Tab and Shift+Ctrl+Tab by cycling through mintty windows. This can be disabled in the options (under Keys), or through a ~/.minttyrc file with:
    SwitchShortcuts=no

    Then the .screenrc should contain:
    # MinTTY sends “\e[1;5I” for Ctrl+Tab and “\e[1;6I” for Ctrl+Shift+Tab.
    bindkey “^[[1;5I” next
    bindkey “^[[1;6I” prev

Comments are closed.