Saturday, February 25, 2012

Making git commands clear screen when they exit

One thing I don’t like about git is that commands like git diff don’t clear screen when they exit.  If I view a file using less, e.g. by running less ~/.zshrc, less would clear the file content from the screen when it exits.  But git diff, git log, etc. don’t.  With some Googling around, I found that:
  • git uses the default pager the user has configured.  On Ubuntu machines it’s less when $PAGER is not defined.
  • You can configure less by defining $LESS variable with the flags you want to pass by default.
  • Nigel McNie has posted a set of flags that are a good default flags for less.
  • -X option to less tells it to not clear the screen when it exits.  From a related question on Super User I found that passing the flag -+X would force less to always clear the screen when it exits.
Combining all these together, I have added this to my ~/.zshrc, and everything is just the way I want.
export LESS="-+X -FR"
While adding this line made git work fine, it resulted in less behaving erratically.  Now I have the following config in my ~/.gitconfig, and things seem to be good.
[core]
  pager = less -+X -+F