Bat: Mouse wheel support in x-terminals

Created on 1 Jul 2020  路  20Comments  路  Source: sharkdp/bat

Hi.
I've been using "less" for years and it supports the mouse wheel for scrolling up and down the displayed text. Could it also be added to bat ?

documentation duplicate question

All 20 comments

Bat does support the mouse wheel, but it's disabled on old versions of less as a side effect of working around a bug with less --quit-if-one-screen. What version of less are you running?

This is also described in the README: https://github.com/sharkdp/bat#using-a-different-pager - but maybe that section could be improved.

Worth noting that on macos, there is an updated version of less available via homebrew (487 bundled with Catalina vs. 551 via brew)

Bat does support the mouse wheel, but it's disabled on old versions of less as a side effect of working around a bug with less --quit-if-one-screen. What version of less are you running?

less 551 (GNU regular expressions)
bat 0.12.1

This is also described in the README: https://github.com/sharkdp/bat#using-a-different-pager - but maybe that section could be improved.

Indeed. In only read the man page, not the readme, my bad. The man page says that when no "--pager" option is used, "The default pager is 'less'. " and that's what I wanted.
So after reading the readme section, I should set $PAGER聽to "less -R" if I had a version of less older that 530, but I have the 551 version. Shouldn't it work out of the box ?

but I have the 551 version. Shouldn't it work out of the box ?

It should. Which terminal emulator are you using? Are you inside a tmux session?

but I have the 551 version. Shouldn't it work out of the box ?

It should. Which terminal emulator are you using? Are you inside a tmux session?

Sometimes yes, sometimes no :)
I just tried with and without inside konsole :

  • without tmux : the scroll-wheel makes the terminal itself scroll up and down
  • with tmux : it works.

The scroll wheel works with less in both cases.

Using iTerm with scroll reporting on, it works both with and without tmux

Using iTerm with scroll reporting on, it works both with and without tmux

Using xterm it doesn't work in both cases.
Using xfce4-terminal, it's the same as with konsole.

Works perfectly fine in konsole 20.04.2 for me.

It does not work in xterm though. I'm not familar with xterm settings, but you might be able to reconfigure it to forward mouse scroll input as up/down key-presses.

If you have a recent version of less (don't remember the exact version), you can also pass the --mouse option to less by adding

--pager="less -FR --mouse"

to your bat config file. This makes things work in xterm for me. We don't do this by default because it breaks selection of text with the mouse.

Hi !

Here to : konsole 20.04.2 and still doesn't work without changes with the --pager option. My less version is the one in debian testing : less 551 (GNU regular expressions). Do you have a special konsole configuration ?

As for xterm, I was just using it for a test, I haven't used it for tenths of years if there was another one available :)

Indeed the less options for --pager break mouse selection.

Can you please post the output of the script at https://github.com/sharkdp/bat/blob/master/diagnostics/info.sh here?

Here it is :

system

$ uname -srm
Linux 5.7.0-1-amd64 x86_64

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux bullseye/sid
Release: testing
Codename: bullseye

bat

$ batcat --version
bat 0.12.1

$ env

bat_config

$ cat /home/thomas/.config/bat/config


# Use italic text on the terminal (not supported on all terminals)
--italic-text=always

bat_wrapper

No wrapper script for 'bat'.

No wrapper script for 'batcat'.

bat_wrapper_function

No wrapper function for 'bat'.

No wrapper function for 'cat'.

No wrapper function for 'batcat'.

tool

$ less --version
less 551 (GNU regular expressions)

My less version is the one in debian testing : less 551 (GNU regular expressions). Do you have a special konsole configuration

I just removed all konsole settings and it still works. You are trying to display a large file (larger than the screen size), right?

Indeed. Files (or command outputs) that are larger than the terminal window size.

so scrolling in less large-file works, but bat large-file, does not (right?). How about

bat --color=always large-file | less -R

so scrolling in less large-file works, but bat large-file, does not (right?).

Exactly.

How about bat --color=always large-file | less -R

Scrolling works perfectly.

I'm out of ideas here. Maybe @eth-p could help?

This is a really odd situation.

When a terminal is set to alternate screen mode, it sends scroll events as keyboard up/down presses instead of scrolling the buffer. This is how less and most editors are able to use the scroll wheel.

Now, a couple theories:

  • Alternate screen mode might not be used unless --mouse is specified. Do you still have a scrollback buffer when using less through bat? Does any text from the pager still remain when you close it? If either of these are "yes", it might not be using the alternate screen.

  • Your terminal is somehow configured to not send key events when scrolling unless terminal mouse reporting is explicitly enabled.

I would assume there shouldn't be a difference between how bat sends data to less and someone using | in the shell to pipe data to less. Yet, they behave differently. Considering that, my theories are more or less just pointless guesses as to why scroll mapping isn't working, and don't really explain how we can fix the bug.

We might need to dig into some source code for both your distro's patched version of less, and bash to find out why less is behaving differently when being piped into from bash compared to being piped into from bat. But, that's going to be extremely difficult unless we can reproduce the issues locally.

  • Alternate screen mode might not be used unless --mouse is specified. Do you still have a scrollback buffer when using less through bat? Does any text from the pager still remain when you close it? If either of these are "yes", it might not be using the alternate screen.

There's an option in Konsole settings window that says "Enable Alternate Screen buffer scrolling". It's enabled. I didn't play with it : was enabled since the beginning. Regarding to scrooblack, I change its value depending on the terminal profile. I had to set it to 8192 lines in order to have all of VLC help without resorting to less/bat :) But otherwise it's the default 1000 lines.

I have 3 profiles where the scrollback buffer is disabled. What I could find out is that using the mouse in bat displaying a file longer than the terminal rows count acts on the terminal output buffer. When no scrollback buffer is enabled, the mouse wheel does not make the terminal go back and forth in its buffer (when using bat, as using less works).

I would assume there shouldn't be a difference between how bat sends data to less and someone using | in the shell to pipe data to less. Yet, they behave differently. Considering that, my theories are more or less just pointless guesses as to why scroll mapping isn't working, and don't really explain how we can fix the bug.

:)

We might need to dig into some source code for both your distro's patched version of less, and bash to find out why less is behaving differently when being piped into from bash compared to being piped into from bat. But, that's going to be extremely difficult unless we can reproduce the issues locally.

Just digging into the distro-specific patches should be enough. Are you sure about bash ?

Anyway, for less a search for mouse or alternate in the changelog doesn't show up anything... Debian specific patches for less are not very numerous. So I'm not sure the solution can be found here...

There are... many many more of them for bash. But neither mouse nor alternate could be found in there.

Konsole has no Debian specific patch.

I took a look at my environment to see if it could have an impact on bat behavior.

  • COLORTERM=truecolor
  • EDITOR=vim
  • KONSOLE_VERSION=200403
  • LESSCLOSE=/usr/bin/lesspipe %s %s
  • TERM=xterm-256color
  • LESSOPEN=| /usr/bin/lesspipe %s
  • GIT_PAGER=cat

Usually I'm using powerline but on some tested profiles a simple PS1 is used.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HakubJozak picture HakubJozak  路  3Comments

x70b1 picture x70b1  路  3Comments

jkaan picture jkaan  路  3Comments

gwpl picture gwpl  路  3Comments

rien333 picture rien333  路  3Comments