how to make less pager respond to scroll wheel and not clear the screen
I know about answers like how-to-make-mouse-wheel-scroll-the-less-pager-using-bash-and-gnome-terminal, which is to remove the -X
flag from the $LESS environment variable. But I would like to do both of these:
- use the mouse wheel to scroll the pager (as opposed to scrolling the terminal window's scrollbar) (which you get by removing
-X
from$LESS
) - have the content from the pager remain on the screen after quitting (which is normally accomplished by adding
-X
to$LESS
).
Is there any way I can have my cake and eat it as well?
Not without hacking less's source code. A bit of background story:
Less cannot handle mouse (including scroll events) at all.
Terminal emulators support a so-called alternate screen. This is which most fullscreen apps switch to for their duration (and switch back to the normal screen when they quit, causing the previous contents to "restore"), and it doesn't have a scrollback buffer. Less also switches to this alternate screen, unless -X
is given in which case it doesn't.
Many terminal emulators figured out that when it's in alternate screen mode and the application running inside is not interested in mouse events, it makes sense to convert scrolling into Up or Down keypress events. It's a hack, and it'd be harmful either on normal screen (imagine what would happen e.g. at your shell prompt), or when the application wishes to handle mouse (sure, they have to see the actual mouse events then). But since by default neither of these two hold when you're running less
, this hack kicks in (subject to the terminal emulator supporting it, and being enabled via \e[?1007h
vs. \e[?1007l
). Your scroll events are converted by the terminal emulator to Up and Down keypresses, and less can't distinguish them from actual keypresses. It doesn't receive mouse scroll events: it sees Up and Down keypresses.
So there you are: Either you switch to alternate screen and the terminal's hack converts scroll events into keypresses for less, and the normal screen is restored when you quit; or you don't, and then there can't be any magic converting scroll events to keypresses and less doesn't understand the scroll events.
So what could be done? Well, either implement mouse support in less and let it handle scroll events itself (and live with a nondefault click or copy-paste behavior), or implement another weird hack: upon quitting, after reverting to the normal screen, less
could for the last time print a screenful of content, repeating whatever was displayed before you quit.
In practice, it basically boils down to: sorry, forget it.
Apparently upgrade to less 530 or newer will get you the feature desired,
http://www.greenwoodsoftware.com/less/news.530.html
These are the differences between version 487 and version 530:
Don't output terminal init sequence if using -F and file fits on one screen.
Didn't find any 'less' PPA that have the newer version, so just download the source pack and build locally and install it.
and configure git like this:
git config --global core.pager 'less -F -S -R -i -+X'
more info: How to use “less -F” without “-X”, but still display output if only one page?