仅当输出超过一页时,如何设置 less 以在退出时清除屏幕?

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

我正在尝试找出一种方法将命令(ag,在本例中)的输出通过管道传输到 less -F(即 --quit-if-one-screen),但是如果输出不到一页,屏幕只是闪烁一下内容,然后才消失。我读过我可以使用 -X (--no-init) 来禁用在退出 less 时清除屏幕,但在那种情况下,长输出也不会被清除,这有点打败了寻呼机的用途。

有没有办法让 less -X-F 一起工作?即,在退出 less 时清除输出,除非输出适合单个页面?

这个问题已经 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

我自己一直在使用它并取得了很好的效果(而且作者对 Github 上的错误报告和功能请求非常敏感),所以我强烈推荐给遇到这个问题的任何人。

现在是 2018 年,530 版中提供了 Less。其中一个关键变化是 less -F 内容小于一个全屏的行为。

解决方案很简单:从您的软件包存储库安装 Less 530,或者 download from Free Software Foundation 并自行编译。然后,如果内容没有填满一整屏,您可以 less -F 将内容留在屏幕上。