113

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.

1
  • Presumably you need the scrolling aspect of less, so more would not be suitable?
    – Svend
    Feb 9, 2010 at 11:48

4 Answers 4

126

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.

11
  • Excellent! -X is what I had in mind. Feb 10, 2010 at 14:31
  • 9
    This is especially annoying if you know about -F but not -X, as then moving to a system that resets the screen on init will make short files simply not appear, for no apparent reason. This bit me with ack when I tried to take my ACK_PAGER='less -RF' setting to the Mac. Thanks a bunch!
    – markpasc
    Oct 11, 2010 at 3:44
  • 10
    This is especially useful for the man pager, so that man pages do not disappear as soon as you quit less with the 'q' key. That is, you scroll to the position in a man page that you are interested in only for it to disappear when you quit the less pager in order to use the info. So, I added: export MANPAGER='less -s -X -F' to my .bashrc to keep man page info up on the screen when I quit less, so that I can actually use it instead of having to memorize it. May 30, 2013 at 19:28
  • 3
    It kinda sucks that you have to decide when you start less how it must behave when you're going to exit. Mar 18, 2014 at 22:00
  • 2
    @MichaelBurr that's because for -X it has to do something special on shutdown as well as startup, so you can only choose this behavior at startup. Apr 26, 2018 at 18:27
28

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
2
  • 3
    In fact, I add export LESS="-XFR" so that the colors show through less as well.
    – dotancohen
    Sep 2, 2014 at 10:12
  • 2
    Thanks for that! -XF on its own was breaking the output of git diff, and -XFR gets the best of both worlds -- no screen-clearing, but coloured git diff output. Jun 10, 2015 at 12:23
10

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).

3

Latest versions

Version 598 (released at the earliest in December 2021) introduces the --redraw-on-quit switch to prevent clearing the screen. Although switches are enabled when starting less, if that switch wasn't enabled on less's startup, it is also possible to enable that feature after by simply typing --redraw-on-quit. I don't have access to a version recent enough to test, but I have it from a most credible source that only typing the 4 first characters should also suffice.

A regular user who would want an even shorter key combination could also add an entry to the lesskey file.

Pre-2022 versions

Versions anterior to 598 can still achieve that thanks to the much older -X switch. 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.

This should be considered as a workaround, as it completely disables initialization, which can cause keys (like Home and End) to stop working.

1
  • It is good to be precise, so thanks for the add. Anyway, it is hard to test indeed ;) none of the boxes I use have such a new version; Latest debian (from dockerhub) has 551, Cygwin I use on one of the boxes has 590 (so close!). I guess it's best to wait, knowing it will be blended into distros. One can ofc install newer 'less' where it's applicable, though my question was rather about a typical box Mar 27, 2023 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .