108

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.

Wojciech Kaczmarek
  • 1,332
  • 2
  • 9
  • 11

4 Answers4

123

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.

sleske
  • 22,652
  • 10
  • 69
  • 93
  • Excellent! -X is what I had in mind. – Wojciech Kaczmarek Feb 10 '10 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`](http://betterthangrep.com/) when I tried to take my `ACK_PAGER='less -RF'` setting to the Mac. Thanks a bunch! – markpasc Oct 11 '10 at 03:44
  • @markpasc: Thanks for pointing that out. I would not have realized that this combination would cause this effect, but now it's obvious. – sleske Oct 11 '10 at 08:45
  • 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. – Michael Goldshteyn May 30 '13 at 19:28
  • but with `-X` now scrolling by arrow keys or with the mouse wheel doesn't work anymore. :/ – K3---rnc Feb 24 '14 at 17:07
  • 3
    It kinda sucks that you have to decide when you start `less` how it must behave when you're going to exit. – Michael Burr Mar 18 '14 at 22:00
  • @K3---rnc: Hm, that's weird. I use `less -X` on multiple computers, and scrolling still works. Consider asking this as a separate question. – sleske Mar 19 '14 at 08:16
  • @sleske, so you're saying that with `LESS=-X man man` scrolling with the **mouse-wheel** still works?? Is any of those systems a Debian? Indeed I should. – K3---rnc Mar 19 '14 at 15:50
  • @K3---rnc: Scrolling with the keys definitely works on Debian. Can't say about the mouse wheel. Just ask a question :-). – sleske Mar 19 '14 at 16:11
  • @sleske here's the question: http://unix.stackexchange.com/questions/167735/can-less-f-be-usefully-combined-with-termcap-initialization – K3---rnc Feb 08 '15 at 17:26
  • 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. – Captain Man Apr 26 '18 at 18:27
26

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
Derek Douville
  • 261
  • 3
  • 3
  • 3
    In fact, I add `export LESS="-XFR"` so that the colors show through `less` as well. – dotancohen Sep 02 '14 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. – Giles Thomas Jun 10 '15 at 12:23
9

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

Lester Cheung
  • 202
  • 2
  • 7
1

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.

  • 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 – Wojciech Kaczmarek Mar 27 '23 at 10:24