4

Is there a way to ensure less clears screen on exit? The opposite of less -X.

The screen is not being cleared when I exit a man page in iTerm2, however the screen is cleared when using the default mac terminal. Does anyone have suggestions?

$LESS is set to less -R

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
jpm
  • 43
  • 3
  • 1
    Buried in the comments to one of the answers is the information that clearing the screen is _not_ what the questioner actually wants. The questioner actually wants to _restore the prior screen contents_, that were there before `less` was run. – JdeBP Jul 24 '16 at 09:33

3 Answers3

5

Normally less "clears the screen" (which probably refers to switching back to the normal screen from the alternate screen) when the terminal description has the appropriate escape sequence in the rmcup capability.

You would see a difference if you are using different values of TERM in the two programs. The infocmp program can show differences for the corresponding terminal descriptions.

less also attempts to clear the remainder of the screen, but that depends upon whether anything was displayed, and if the output was a terminal (in contrast to a pipe).

Aside from the terminal description, some terminal emulators make it optional whether to allow the alternate screen. You may have selected that option at some point. (I'm testing with default configuration, which works as intended).

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
  • To clarify, I want to switch back to the original screen and restore the previous window content history and placement after exiting the altscreen. – jpm Jul 24 '16 at 00:56
  • Sure: that's because your terminal description doesn't switch to/from alternate screen. – Thomas Dickey Jul 24 '16 at 00:56
  • By the way, some terminal emulators make it optional whether to allow the alternate screen. You may have selected that option at some point. (I'm testing with default configuration, which works as intended). – Thomas Dickey Jul 24 '16 at 01:04
  • You are correct. Unselecting `Save lines to scrollback in alternate screen mode` under Profiles > Terminal solved the problem. Thanks. – jpm Jul 24 '16 at 01:09
-1

Why not just clear the screen on exit?

man man; clear
HalosGhost
  • 4,732
  • 10
  • 33
  • 41
Trevor Hickey
  • 947
  • 3
  • 9
  • 17
  • Because even though that's what the questioner wrote, it's not what xe actually wants and the question was sloppily worded. – JdeBP Jul 24 '16 at 09:31
-2

I would suggest if 'less' fails to clear the screen that you put:

function less(){
    less $@
    clear
}

in your ~/.profile or /.bashrc. Not sure if this will behave like you want in pipes, but for other purposes it should be okay.

One could add printf '\033[2J\033[H' to this, which should work on most terminals without relying on terminfo or other external references. It may not work everywhere, though. (Reference: https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html )

(Original answer was utterly wrong, my mistake. This one should actually work, but I can't change the existing downvotes :\)

Wyatt Ward
  • 3,962
  • 2
  • 14
  • 21
  • 1
    That would prevent passing arguments to `less`. Better as `less() { command less "$@"; ret=$?; clear; return "$ret"; }`. Note that `~/.profile` is a session customization file, not a shell customization file. In any case, neither an alias nor a function would affect `man`. – Stéphane Chazelas Jul 24 '16 at 09:04
  • ah, thanks. Looking back that is a much cleaner solution. – Wyatt Ward Jul 27 '16 at 04:19