smartless: a better pager for small and large inputs

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.

The pager 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:

smartless demo

Quick demo of smartless in action. (Unfortunately the recording software I used introduced some artifacts and non-smooth transitions.)

This works great for me, and I have completely replaced less with smartless on my system. For instance, to change git to use the new pager, run

$ git config --global core.pager 'smartless'

Similarly, I have configured an alias so that I can keep typing less, but actually use smartless instead:

alias less='/path/to/smartless'

Download

smartless is a small Bash script, and available on GitHub. Give it a try if it sounds useful, and let me know what you think!

git clone https://github.com/stefanheule/smartless.git

Finally, credit where credit is due: When I originally searched for a solution I found this StackExchange post that my script is based on.

Questions or comments? Send me an email or find me on twitter @stefan_heule.