Scroll with mouse in ‘less’ without clearing the screen

scrollingtrackpad

When viewing a long file in Terminal with less (or man, which uses less internally), I can use two-finger gesture to scroll up and down the file; it's great. But less on OS X has an annoying problem: When I quit, it resets the screen back to what it looked like before I started less.

% date
Sun 30 Oct 2016 16:18:23 CET
% man less
% (hmm... where did my output go?)

When I quit I want to see the next prompt appear below the last screenful I was viewing in less. I can achieve this by using less -X or setting LESS=-X, which suppresses the terminal initialization sequences. Unfortunately, -X also turns off the mouse-scrolling feature. Is there a way to suppress the screen clearing but still be able to scroll with the trackpad?

As I understand it, this would require using a different set of terminfo settings. I cannot make heads or tails of the whole terminfo infrastructure. Does anyone have a solution? I'm on El Capitan, in case it makes any difference.

Best Answer

This question is nearly identical to a question asked on SuperUser. Here's a portion of the answer:

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.