brew install less
git config --global core.pager "less -+\$LESS -RSF"
Installing a newer version of less fixes the bug where -F
causes small outputs to entirely disapear without -X
, and then the config command removes -X
, which is what breaks scrolling.
This will replace the less
version MacOS ships with with the latest, but as they are the same utility (unlike GNU coreutils vs. BSD coreutils), this should not break things.
Explanation
This is an extension to ognockocaten's answer.
That answer suggests:
git config --global core.pager "less -+\$LESS -RS"
This removes two options:
-F
/ --quit-if-one-screen
:
Causes less to automatically exit if the entire file can be displayed on the first screen.
and -X
/ --no-init
:
-X 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.
This works for the question at hand, but is suboptimal, as less
will always switch to an alternate page regardless of how small the output is. So, even for small commands like git diff
, you'll get a full alternate screen.
Reading closely, it seems like re-adding -F
would fix this. However, a combination of two things causes this to break:
- on the version of less MacOS ships with,
487
, less will switch to the alternate screen first, before checking if the text is small enough, and thus print the output to the alternate screen buffer. This was fixed in version 530
(see https://unix.stackexchange.com/a/107355/39135)
- on MacOS, without
-X
, switching back from the alternate to main buffer clears the screen and thus erases all text that was just printed to the alternate screen buffer.
So just re-adding -F
, you'd get small outputs being completely erased, as if they were never printed. We can't re-add -X
to fix this, as that breaks scrolling, but we can update to a newer version of less
!
Thus it all comes together - we can use homebrew to install the latest stable less
version, and then configure the pager to include the -F
option, as per the command block at the start of this post.
space
orw
?export LESS=-R
to~/.zshrc
file works for me.