Skip to content

Instantly share code, notes, and snippets.

@MatthiasPortzel
Last active January 31, 2023 00:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthiasPortzel/8908120d406f37e92fbf0412149e63b0 to your computer and use it in GitHub Desktop.
Save MatthiasPortzel/8908120d406f37e92fbf0412149e63b0 to your computer and use it in GitHub Desktop.

Making git diff and less work together nicely on Mac

For reference, some Less options we're going to be using:

  • -S, less doesn't wrap lines;
  • -F, less auto-exits if output less than a page
  • -R, less honors color control codes
  • -X, disables termcap. This is set by git by default, but it breaks scrolling for some reason
  1. Make sure you're using the most recent version of both software. (The versions that are compiled by Apple and included in macOS are non-standard.)
brew install less
brew install git
  1. Set the LESS enviroment variable. This variable includes the flags that are passed to Less by default. In my .zshrc:
export LESS="SF"
  1. Setting this variable disables the git defaults, so we have to reset Git's pager setting.
git config --global core.pager "less -RF"
@MatthiasPortzel
Copy link
Author

MatthiasPortzel commented Oct 7, 2022

If using these Less settings, by default in iTerm2, opening less and scrolling down will replace previous lines of your terminal scrollback with the file contents. I find this behavior unintuitive and undesired. It can be disabled by un-checking "Save lines to scrollback in alternate screen mode" in iTerm2 preferences > Profile > Terminal. (Ref)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment