Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
Presumably you need the scrolling aspect of less, so more would not be suitable? –  Svend Feb 9 '10 at 11:48

2 Answers 2

up vote 38 down vote accepted

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.

share|improve this answer
    
Excellent! -X is what I had in mind. –  Wojciech Kaczmarek Feb 10 '10 at 14:31
4  
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 '10 at 3: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 8:45
2  
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 at 17:07

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.