Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created July 13, 2012 10:39
Show Gist options
  • Save dansimau/3104183 to your computer and use it in GitHub Desktop.
Save dansimau/3104183 to your computer and use it in GitHub Desktop.
Enable git-style colour output in regular diff on Mac OS X

Enable git-style colour output in regular diff

Mac OS X

  1. Install colordiff using Homebrew:

     brew install colordiff
    
  2. Add function to your ~/.bash_profile:

     cat <<EOF >>~/.bash_profile
    
     function diff {
         colordiff -u "$@" | less -RF
     }
     EOF
    
  3. Change default output colours to match git:

     cat <<EOF >~/.colordiffrc
     newtext=green
     oldtext=red
     diffstuff=cyan
     EOF
    
  4. Reload:

     . ~/.bash_profile
    
  5. Profit.


Notes on switches:

  • -u in diff means: unified diff
  • -R in less means: show raw escape codes (so we can see the colour)
  • -F in less means: quit if entire output fits on one screen
@harrybiddle
Copy link

One thing that colordiff supports and git diff doesn't is a feature I use very often: diff'ing the output of commands:

$ colordiff <(echo hello, world) <(echo goodbye, cruel world)
1c1
< hello, world
---
> goodbye, cruel world

whereas

$ git diff <(echo hello, world) <(echo goodbye, cruel world)
error: /dev/fd/63: unsupported file type
fatal: cannot hash /dev/fd/63

@johnpancoast
Copy link

johnpancoast commented Sep 19, 2021

I suppose it's not related to git but if you want similar diffs and have VIM there is another solution shown here.

diff file1 file2 | vim -R -
-or-
diff file1 file2 | view -.

The view command is same as vim -R.

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