A pager is a computer program that makes viewing long output or long files on the terminal more pleasant. Instead of dumping all the output at once, a pager shows the text one page at a time, allowing the user to scroll up and down, or search through the text. For instance, viewing a large diff with
git is much more pleasant with a pager. The most common pager on Unix systems is probably
less, and while it works great in many cases, viewing only very short output in a pager is a bit silly. If all of the text is only a few lines long and fits on the screen, then no pager is required in the first place.
However, it is often hard to know upfront whether a pager will be necessary, and so it would be more convenient to always use a pager and have the pager return immediately if the text is short, and just print it instead.
less has a command line option
-F that does exactly this. Unfortunately, it comes at a price: scrolling is no longer possible. For this reason, I have built my own pager I call
smartless, that avoids exactly this problem. For small output,
smartless will just print the text directly and exit.
For larger output (more than 15 lines by default, but this is configurable), the pager
less is invoked. Note that the first 15 lines are still printed directly, before
less is called. This is a feature and not a bug, as it gives a little bit of information about the output, even after exiting
less. Here's a little demo:
This works great for me, and I have completely replaced
smartless on my system. For instance, to change
git to use the new pager, run
Similarly, I have configured an alias so that I can keep typing
less, but actually use
Finally, credit where credit is due: When I originally searched for a solution I found this StackExchange post that my script is based on.