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

81

14

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

Posted 2010-02-09T11:21:00.057

Reputation: 1 022

Presumably you need the scrolling aspect of less, so more would not be suitable? – Svend – 2010-02-09T11:48:34.657

Answers

97

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

Posted 2010-02-09T11:21:00.057

Reputation: 19 887

Excellent! -X is what I had in mind. – Wojciech Kaczmarek – 2010-02-10T14:31:02.800

@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 – 2018-04-26T18:27:14.650

7

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 – 2010-10-11T03:44:15.610

@markpasc: Thanks for pointing that out. I would not have realized that this combination would cause this effect, but now it's obvious. – sleske – 2010-10-11T08:45:14.163

7This 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 – 2013-05-30T19:28:41.100

but with -X now scrolling by arrow keys or with the mouse wheel doesn't work anymore. :/ – K3---rnc – 2014-02-24T17:07:48.313

2It kinda sucks that you have to decide when you start less how it must behave when you're going to exit. – Michael Burr – 2014-03-18T22:00:05.890

@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 – 2014-03-19T08:16:10.403

@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 – 2014-03-19T15:50:51.933

@K3---rnc: Scrolling with the keys definitely works on Debian. Can't say about the mouse wheel. Just ask a question :-). – sleske – 2014-03-19T16:11:21.727

19

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

Posted 2010-02-09T11:21:00.057

Reputation: 191

2In fact, I add export LESS="-XFR" so that the colors show through less as well. – dotancohen – 2014-09-02T10:12:43.763

2Thanks 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 – 2015-06-10T12:23:06.280

3

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

Posted 2010-02-09T11:21:00.057

Reputation: 133