less -F
closes less if the contents fit on one screen. However, if I use termcap initialization, i.e., I don't use -X
and the file fits on the screen, nothing is printed because less displays the contents, notices that they fit on the screen, exits and clears the screen during deinitialization.
This can be demonstrated as follows (assumes empty LESS
variable):
$ echo foo > new_file
$ cat new_file
foo
$ less -X -F new_file
foo
$ less -F new_file
$
I like termcap features, it allows scrolling with the mouse wheel among other things so I definitely don't want to use -X
. Is there still a way to have less quit if the content fits on the screen (and basically behave like cat
)?
Best Answer
Here's a relevant excerpt from
less
' FAQ: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
, likelyxterm
) is configured with: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:
and then let the terminfo library know this is the new, preferred capability spec for $TERM,
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):