43

I'm using less as my Git pager.

If the git diff output is readable on one page, my Git prints the output to the screen.

Sometimes I'm too fast with typing Ctrl + D (half page down), which kills my terminal. Is there an option to enable the pager for git diff, even if the output is very small?

This doesn't work:

  • git -p diff
  • git --paginate diff
  • git settings: pager.diff = true
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave Halter
  • 15,556
  • 13
  • 76
  • 103

1 Answers1

64

This is controlled by the -F (--quit-if-one-screen) option to less.

Git uses the options FRSX for/of less by default, if none are specified by the $LESS or $GIT_PAGER environment variables. To change it, specify the core.pager option and set it to RSX:

git config --global core.pager 'less -+F'

Older versions of Git used to recommend the following in their documentation:

git config --global core.pager 'less -+$LESS -RSX'
knittl
  • 246,190
  • 53
  • 318
  • 364
  • git does not use FRSX by default. I suspect you are getting those defaults from the environment variable `LESS`, or from `GIT_PAGER`, which suggests another (imo simpler) solution. Namely, ensure that `F` does not appear in `LESS` or in `GIT_PAGER`. If git does use defaults when PAGER and GIT_PAGER are unset, that would surprise me, but perhaps I'm wrong about that. Documentation? – William Pursell Nov 21 '12 at 19:16
  • 1
    @WilliamPursell: The Git Documentation has been recently updated to disable options in a more backwards compatible way. My code was directly copied from the (old) man page – I have updated it now. The documentation also writes »Git sets the LESS variable to FSRX if it is unset« – knittl Nov 22 '12 at 14:20
  • @Leonmax: [git help config](https://www.kernel.org/pub/software/scm/git/docs/git-config.html) – knittl Oct 01 '13 at 19:54
  • 1
    May want to add '-r' to enable interpreting ANSI colors. – dset0x Nov 26 '14 at 12:38
  • `-E` is another nice option. It actually quits `less` if the page is less than a screen. I thought `-F` alone would do that, but you also need `-E` (`--QUIT-AT-EOF`). – Geremia Jan 25 '16 at 20:21
  • 6
    You may also want `-c`, which causes the output to always start at the top and fill the screen. – Chris Martin Nov 07 '16 at 01:01
  • I use [diff-so-fancy](https://github.com/so-fancy/diff-so-fancy) which by default uses options `-RFX` for less. This answer pointed me in the right direction, I removed the `-F` from the options and now get what I want. – verboze Sep 28 '17 at 16:21
  • 1
    Do not ever use `-r` unless you really know what you're doing, always prefer `-R`. I just spent like half an hour debugging this. "`-R`" means "pass through escape codes that less understands, and update less's model of the terminal." Whereas "`-r`" means "pass through all escape codes and ignore them; good luck." (This generally mangles the output in subtle ways.) – Glenn Willen Jun 17 '20 at 04:55