How to use mousewheel in GNU Screen

GNU Screen has support for scrollback, but by default you have to use awkward keys to use it. I would like to be able to use Shift-PageUp, Shift-PageDown and the mousewheel to scroll, just like you can do in xterm.

It was not easy to configure Screen for this, and it involves cooperation with the terminal emulator. But I finally managed to achieve a solution which works pretty well. Add this to your ~/.Xresources file (create it f you don’t have it already) (you need to log out for this to take effect):

XTerm*saveLines: 0
XTerm*vt100.translations: #override \n\
  Ctrl <Btn4Down>: string(0x1b) string("[25S") \n\
  Lock Ctrl <Btn4Down>: string(0x1b) string("[25S") \n\
  Lock @Num_Lock Ctrl <Btn4Down>: string(0x1b) string("[25S") \n\
  @Num_Lock Ctrl <Btn4Down>: string(0x1b) string("[25S") \n\
  <Btn4Down>: string(0x1b) string("[5S") \n\
  Ctrl <Btn5Down>: string(0x1b) string("[25T") \n\
  Lock Ctrl <Btn5Down>: string(0x1b) string("[25T") \n\
  Lock @Num_Lock Ctrl <Btn5Down>: string(0x1b) string("[25T") \n\
  @Num_Lock Ctrl <Btn5Down>: string(0x1b) string("[25T") \n\
  <Btn5Down>: string(0x1b) string("[5T") \n\
  Shift <KeyPress> Prior: string(0x1b) string("[25S") \n\
  Shift <KeyPress> Next: string(0x1b) string("[25T") \n

Then add this to your ~/.screenrc file:

defscrollback 1000

# Scroll up
bindkey -d "^[[5S" eval copy "stuff 5\025"
bindkey -m "^[[5S" stuff 5\025

# Scroll down
bindkey -d "^[[5T" eval copy "stuff 5\004"
bindkey -m "^[[5T" stuff 5\004

# Scroll up more
bindkey -d "^[[25S" eval copy "stuff \025"
bindkey -m "^[[25S" stuff \025

# Scroll down more
bindkey -d "^[[25T" eval copy "stuff \004"
bindkey -m "^[[25T" stuff \004

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

Note that this disables the normal scrolling support in xterm, you will only be able to scroll when using Screen. You might want to start xterm like this to always use Screen:

xterm -e screen

Edit: Create ~/.Xresources if you don’t have it already.

This entry was posted in Linux. Bookmark the permalink.

12 Responses to How to use mousewheel in GNU Screen

  1. Saul says:

    Hi. This simple line in .screenrc works for me in konsole (Kubuntu 8.04):

    termcapinfo xterm|xterms|xs|rxvt ti@:te@

  2. anonymous says:

    @Saul That doesn’t work when you are swapping back and forth between multiple screen windows. All of the output from those screen windows gets jumbled together in the xterm scrollback buffer. Screen has it’s on per-window scrollback buffers that are kept separate though. This enables you to use the screen scrollback buffers instead of the xterm one.

  3. Jon z says:

    Awesome! I can confirm this works on ubuntu, still not sure how to achieve this effect on OSX.. all that Xresources stuff is total voodoo to me.

  4. Quantum says:

    Whoa…. it would be cool to get it working under OSX… I’ll try it out this weekend 🙂

  5. Sergey says:

    Cool! It really works! Could you suggest same hack for rxvt-unicode?

  6. Sorry, I have never used rxvt.

  7. Andre Klärner says:

    If someone stumbles over this page once again searching for “mousewheel gnu screen” and would like to find an answer: It took some time, but I figured it out.

    The screenrc-snipped is the same as above, but I left out the “mousewheel scrolls in screen immediately” part and replaced it with “mousewheel scrolls x-lines” so you have to enter copy mode beforehand.

    The relevant part for .Xressources is:
    URxvt*saveLines: 0
    URxvt*scrollBar: false

    urxvt*keysym.S-Prior: 33[25S
    urxvt*keysym.S-Next: 33[25T

    This disables the urxvt-scrollback and it’s scrollbar. It also maps the Shift-PageUp and Shift-PageDown to the sequences as in the screenrc-part.

    This up to now only enables the use of Shift-PageUp and Shift-PageDown to enter copy mode and scroll. The mouse part requires the extension “vtwheel” in a slightly patched version.

    Add this as a file named “vtwheel” to ~/.urxvt/ext/ (or anywhere else in the searchpath for urxvt-perl-extensions):

    #! perl

    # Implements a scrollwheel just like in good old vt100's mices

    sub simulate_keypress {
    my ($self, $type) = @_; #type: 0:up, 1:down

    my $keycode_up = 111;
    my $keycode_down = 116;

    my $numlines = 8;

    my $keycode = 0;
    if ($type eq 0) {
    $keycode = $keycode_up;
    } elsif ($type eq 1) {
    $keycode = $keycode_down;
    } else {

    for (my $i = 0 ; $i ne $numlines ; $i++) {

    sub on_button_release {
    my ($self, $event) = @_;

    #my $res_ss = $self->resource("secondaryScroll");
    #warn("ressource ss is ");

    #!$self->current_screen and return ();

    #warn("foo, event: {button}>\n");
    if ($event->{button} eq "4") { # scroll up
    } elsif ($event->{button} eq "5") { # scroll down

    return ();

    I modified the original code to disable the check for the secondaryScreen as screen seems not to use it in the containing terminal. I also increased the lines to scroll per mouse-scroll to 8, as I am a bit more used to that.

    Then you have to enable this extension as usual with URxvt.perl-ext-common: …,vtwheel in your .Xresources. If you are using xrdb (probaply) you can reload it with “xrdb -merge .Xresources”.

    Then start a new instance of urxvt with screen and have fun!


  8. marcin says:

    I’m using Linux Mint 18.2 Cinnamon and I have no ~/.Xresources file. Should I create it and paste above?

  9. Yes, just create ~/.Xresources if you don’t have it already.

  10. marcin says:

    Ok, it works when i’m on Linux machine, but in PuTTY (remote windows machine) it doesn’t work…

  11. This is for xterm, PuTTY is not xterm.

Comments are closed.