brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: less -F skips the pager when the output fits on one screen

Less
~/tips/tip-less-f-quit-if-one-screen

some-command | less -F

Makes less exit by itself when the whole input fits on the first screen.

A pager is great for long output and a nuisance for short output. I don’t want to press q to see three lines. less has an option that decides for you.

some-command | less -F

The manual says -F (--quit-if-one-screen) makes less exit automatically if the entire file can be displayed on the first screen. A two-line input printed both lines and returned straight to the prompt on less 590, with exit code 0, and a long input opens as a normal pager.

You can put the option in the LESS environment variable, which less reads before the command line, so options on the command line still win:

export LESS=-FRX

Here -R passes ANSI colour codes through in a limited way, so coloured output from ls or git stays coloured. -X disables sending the termcap initialization and deinitialization strings to the terminal. It is the one to add if the output of a short file disappears when less quits on your terminal.

One caveat: the variable is parsed before the command line, so an option you type still overrides it. The manual also says that if -r appears in LESS, it is treated as -R. Whichever pager options you set there apply to every program that runs less, so keep the list short.

Further reading: less(1) manual

More Less tips less -i /var/log/syslog less +F /var/log/syslog All Less tips (3) All topics

Link