waldyrious waldyrious - 11 months ago 41
Bash Question

How to set less to clear the screen on exit only if the output fills more than a single page?

I'm trying to figure out a way to pipe the output of a command (

, in this case) to
less -F
), but if the output is less than one page, the screen just flashes the content before it disappears. I've read that I can use
) to disable clearing the screen upon exiting
, but in that case long output doesn't get cleared either, which kinda defeats the purpose of a pager.

Is there a way to make
less -X
work with
? I.e., to clear the output upon exiting less, except if the output fits in a single page?

Answer Source

This very question has been answered in Unix.SE. The top-voted answer there has actually been expanded into a full-fledged command-line tool that can act as a replacement for less: https://github.com/stefanheule/smartless.

I've been using it myself with great results (plus the author is very responsive to bug reports and feature requests on Github), so I highly recommend it to anyone facing this issue.