1

I recently had to setup a Windows computer. I usually remote into a linux box(Ubuntu) for development, so I'm using "Windows terminal" app with Window's built-in ssh client to login to my Linux box. Now I notice that I cannot use the trackpad/mouse to scroll through

  1. git logs
  2. man pages
  3. git diffs

I have to resort to arrow presses, which is not my preferred approach. I haven't been able to figure out who is responsible for paging in this workflow -

  • Windows terminal
  • Window's ssh client
  • My Linux box's pager settings (default pager is set to less there)

I have tried Alacritty on Windows with the same result, I have also tried WSL Ubuntu(within Windows terminal) to ssh login into the remote Linux box, but in vain.

Could someone help me understand how I could enable paging support/trackpad-mouse scrolling support in this setup?

rookie
  • 97
  • 8

2 Answers2

6

It's both, in a way.

Although the terminal has its own "scrollback" (which you can access with mouse or scrollbar), full-screen programs – including pagers like 'less' – deliberately disable it while running (they enable the so-called alternate screen mode for full-screen operation), because they wouldn't have enough control over the terminal's scrollback. So instead the terminal-level scrolling is disabled in this mode, and all scrolling/paging is done custom by 'less'.

(In a sense, this is the whole point of using an in-terminal pager, instead of just dumping the whole text to terminal and scrolling it at terminal level. By doing scrolling internally, 'less' can start at the top of the file and let you scroll down, instead of always ending up at the bottom.)

Your main problem is that the 'less' pager simply doesn't have mouse support. (Terminal programs can request to directly receive mouse input1 – e.g. you will find native mouse scroll-wheel support in Vim or Elinks or WeeChat – but the 'less' pager does not do this.)

On a full Ubuntu environment, when you're somehow able to scroll with the wheel inside 'less', that's actually the terminal translating wheel events to fake Up/Down arrow keypresses – GNOME Terminal always does this when the alternate-screen mode is active, if the app hasn't activated direct mouse input.

(As a practical example, if you manually ask the terminal to switch to altscreen mode with tput smcup, and if you're using GNOME Terminal, then the mouse wheel will begin scrolling through your shell history. tput rmcup gets you back.)

So the secondary problem is that Windows Terminal doesn't provide wheel event emulation, either. It does support sending real wheel events to mouse-supporting programs (again, you can wheel-scroll in Vim while using Windows Terminal), but wheel-to-arrows is mostly unique to GNOME Terminal and other vte-based terminals.

Your options are:

  • Open a feature request at https://github.com/microsoft/terminal/ for the scroll emulation feature (if there isn't one already).
  • Use a pager with native mouse-event support (e.g. PAGER=elinks or vimpager to use Vim as a pager).
  • Use GNOME Terminal (or another vte-based terminal) through WSLg or X11 instead of Windows Terminal.
  • Don't use a pager at all, relying on the terminal's scrollback.

1 However, due to the way terminal APIs work in Windows, SSH is not 100% transparent (e.g. ssh.exe has to deliberately request "Unix-like terminal" mode rather than legacy "WinNT console" mode), and importantly here, older versions of OpenSSH for Windows (7.4p1, I think) were unable to relay any mouse events to the remote side. This has since been fixed – if you're at least on Windows 20H2 and have OpenSSH 8.1p1 or newer.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 1
    "mostly unique to GNOME Terminal and other vte-based terminals" – I think `tmux` can do the job ([example](https://superuser.com/a/1319237/432690)). – Kamil Maciorowski Apr 22 '22 at 18:50
  • Thanks for the answer, it's quite comprehensive. If I understood this correctly, then I should ideally be able to map a scroll event to arrow keys and get this too work as expected, without having to request for the feature in Windows Terminal? – rookie Apr 22 '22 at 19:06
  • @rookie: Depends on where you want the mapping to occur... If you patched 'less' to at least _request_ the mouse events (terminals do not send those by default, OTOH there are like 6 different modes for that), then yes, you could do that – although it wouldn't be too far to make it directly handle scroll events without the indirection. 'less' is ncurses-based, and ncurses already has mouse support to some extent, so it should be possible. – u1686_grawity Apr 22 '22 at 19:10
  • 1
    Thanks! Looks like alternate screen support is not too far from being availabe: https://github.com/microsoft/terminal/pull/12569 – rookie Apr 22 '22 at 19:27
  • 1
    No, alternate screen support has been available for a _long_ time in Windows Terminal. What's not available is the translation of wheel to fake arrow keys. (The only relation between the two is that such translation would typically be useful only for the kind of apps that _also_ use altscreen mode, which is why GNOME Terminal limits it to that mode.) – u1686_grawity Apr 22 '22 at 19:31
  • Ah, I overlooked that the GitHub PR is for altscreen _scroll_ mode. Yes, that's basically the translation that GNOME Terminal does, although in this case it's opt-in instead of automatic (so 'less' would have to request it). – u1686_grawity Apr 22 '22 at 20:05
  • 2
    Since nothing indicates that the answerer or the asker acknowledged my comment about `tmux`, I will explain: I believe `tmux` is yet another option. @rookie ! [Use](https://superuser.com/a/1492018/432690) [`tmux`](https://superuser.com/a/1398132/432690) ([on the server](https://superuser.com/a/1288684/432690)) and configure it [like this](https://superuser.com/a/1319237/432690) (don't miss the comment about `git`). – Kamil Maciorowski Apr 23 '22 at 04:12
  • Thanks for bringing my attention to your comment. I just tried the suggested answer that you linked. Unfortunately my trackpad still doesn't scroll inside tmux. Perhaps something is still amiss – rookie Apr 23 '22 at 05:23
  • 1
    @rookie I have just realized the linked answer misses one thing: you need to configure `tmux` to use the mouse in general. Usually people configure `tmux` like so in the first place, they find something is amiss, they search and *then* they find the linked answer. Please make sure your `~/.tmux.conf` contains `set -g mouse on` before the code from the linked answer. Exit `tmux` totally (so `tmux a` tells you there is no server running) and start `tmux` anew. – Kamil Maciorowski Apr 23 '22 at 08:15
1

It is pretty simple on Windows to convert mouse actions to scroll keys using the free AutoHotkey.

A simple AutoHotkey script for that is easy to write, but a more sophisticated and configurable AutoHotkey script is available at DragToScroll.

To run, after installing AutoHotKey, download the .zip of the DragToScroll repository and unzip to a folder, then run DragToScroll.ahk by a double-click on it. By default, the scroll is done by dragging the window using the right mouse/trackpad button.

You may stop the script by right-click on the green H icon in the traybar and choosing "Exit".

You may also right-click the H icon and choose "Settings" to configure the script. You may configure the scroll method, its speed, limit its actions to a single process (for example powershell.exe), and more.

To have it run on login, place it in the Startup group at
C:\Users\USER-NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

harrymc
  • 455,459
  • 31
  • 526
  • 924