Here's a relevant excerpt from less
' FAQ:
Can less leave the screen alone when it quits?
When less starts, it sends the "ti" termcap string to the terminal,
and when it quits, it sends the "te" string. Exactly what the "te"
string does depends on the system you are using, the type of terminal,
and your version of termcap. On some systems, it does nothing; on
others it restores the screen to the state it was in when less
started, thus erasing from the screen the last page of the file which
less was viewing.
If you don't like the behavior of "te" on your system, you can disable
the sending of the "ti" and "te" strings by invoking less with the -X
option. Unfortunately, this sometimes has other side effects, because
the "ti"/"te" strings might do other things that are required. For
example, on some terminals, disabling "ti" and "te" causes arrow keys
to cease to function.
If you want "te" to do something different (for example, restore the
screen if it's not doing that already), you'll have to figure out how
to change the termcap or terminfo for your terminal on your system.
Unfortunately, this is done differently on different systems, so
you'll have to check the documentation for your system.
Research leads to "ti" and "te" termcap capabilities being named "smcup" and "rmcup" in terminfo, which is a more modern termcap equivalent.
You can see how your terminal ($TERM
, likely xterm
) is configured with:
infocmp -1 # which will print out capabilites one per line
You can decipher the meaning of sequences with the help of a VT100 control characters reference.
If you strip the "smcup" and "rmcup" capabilities from your terminal:
infocmp -1 | sed -r '/[sr]mcup.*/d' > new-terminfo-for-$TERM
and then let the terminfo library know this is the new, preferred capability spec for $TERM,
tic new-terminfo-for-$TERM
# you reverse this with `rm -ri ~/.terminfo`
you will notice the less
' alternative screen is not cleared anymore (because it had never been entered). But so does the mouse not work anymore.
If you fiddle with "rmcup" control character sequences and figure it out, please let me know!
Update:
A working alternative is to wrap less as a function that runs cat when the file fits the terminal height (put this in your .bashrc):
less () {
# When single file argument, and file small enough, cat it
if [ $# = 1 ] && [ -f "$1" ] && [ $(wc -l "$1" | cut -d' ' -f1) -le $((${LINES:-0} - 3)) ]; then
cat "$@"
else
command less "$@"
fi
}
Well... that would be against the idea of paging... wouldn't it? :-)
But to answer your question: I'm pretty sure there isn't. This is from the source code of less:
/*
* The char won't fit in the line; the line
* is too long to print in the screen width.
* End the line here.
*/
if (chopline || hshift > 0) <--- you have chop lines (-S)
{
...
quit_if_one_screen = FALSE; <--- this resets -F
}
Sorry :-)
Best Answer
Since less 530 (released in December 2017),
less --quit-if-one-screen
does not switch to the alternate screen if it reads less than one screenful. So you won't have this problem if your version of less is recent enough.In earlier versions, less has to decide whether to use the alternate screen when it starts. You can't defer that choice to when it terminates.
You could call less, let it use the alternate screen, and cat the content onto the primary screen if less terminates automatically. However I don't know of a way to detect automatic termination.
On the other hand, it isn't that difficult to call cat for short inputs and less for larger inputs, even preserving buffering so that you don't have to wait for the whole input to start seeing stuff in less (the buffer may be slightly larger — you won't see anything until you have at least one screenful of data — but not much more).
You might prefer to see the lines on the main screen as they come in, and switch to the alternate screen if the lines would cause scrolling.