'less' command clearing screen upon exit - how to switch it off?

How to force the less program to not clear the screen upon exit?

I'd like it to behave like git log command:

  • it leaves the recently seen page on screen upon exiting
  • it does not exit the less even if the content fits on one screen (try git log -1)

Any ideas? I haven't found any suitable less options nor env variables in a manual, I suspect it's set via some env variable though.


To prevent less from clearing the screen upon exit, use -X.

From the manpage:

-X or --no-init

Disables sending the termcap initialization and deinitialization strings to the terminal. This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.

As to less exiting if the content fits on one screen, that's option -F:

-F or --quit-if-one-screen

Causes less to automatically exit if the entire file can be displayed on the first screen.

-F is not the default though, so it's likely preset somewhere for you. Check the env var LESS.


If you want any of the command-line options to always be default, you can add to your .profile or .bashrc the LESS environment variable. For example:

export LESS="-XF"

will always apply -X -F whenever less is run from that login session.

Sometimes commands are aliased (even by default in certain distributions). To check for this, type

alias

without arguments to see if it got aliased with options that you don't want. To run the actual command in your $PATH instead of an alias, just preface it with a back-slash :

\less

To see if a LESS environment variable is set in your environment and affecting behavior:

echo $LESS

Or just set it in your global git config:

git config --global core.pager 'less -FX'

This way other tools are unaffected (which I like).