Mac OS X color showing ESC[whatever for git-diff colors (and more)

Since my upgrade to OS X 10.5 I have been having a terrible time getting colors to work with git.

Originally I was using OS X 10.5.4, git version 1.5.3.1, and iTerm or Terminal.app. $TERM=xterm or xterm-color. I have color with PS1, ls, vim and most git tasks. However, git diff (or git show) always screws up the diff by showing the ESC[ characters instead of colors. For instance:

  [nathan@nate ~/s3]$ git show 67d254ec17f
  ESC[33mcommit 67d254ec17fdc507765ddee1feb2a14e5896e79fESC[m

I searched and searched wasn't able to figure out how to fix this. It was suggested on the github forum that I upgrade to git 1.5.6.4. Unfortunately not only did it not fix the color in git diff, it broke the color in git status which was working previously.

After about a week of poking around on and off I discovered that the problem was not git, but my GIT_PAGER which is more. If I tried GIT_PAGER=cat git-diff then the color output just fine. less and more use the same environment variable LESS (man less for details). And less was outputting the colors as raw escape codes.

man less shows that the option -R is defined as follows:

       -R or --RAW-CONTROL-CHARS
              Like  -r,  but  only  ANSI "color" escape
              sequences are output in "raw" form.

That would do it.

After perusing the man page for a while I settled on adding this to my
~/.profile:

  export LESS="-erX"

At the end of the day, this tip is really about less and how it interprets colors and probably has little to do with git. Even so, I only found this odd behavior while using git. Hopefully this post will help someone who is having similar issues.

As a side note, below is an example of how to configure your colors for git-diff and other commands.

[user]
    name = Your Name
    email = y...@yours.com
[color]
    branch = auto
    status = auto
    diff = auto
[color "diff"]
    meta = yellow
    frag = cyan
    old = red
    new = green
[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green
[color "status"]
    added = yellow
    changed = green
    untracked = cyan
[alias]
    st = status
    ci = commit
    co = checkout
    br = branch 
Share:
  • del.icio.us
  • Reddit
  • Technorati
  • Twitter
  • Facebook
  • Google Bookmarks
  • HackerNews
  • PDF
  • RSS
This entry was posted in tips and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • mc

    Thank you for this post, I’m not using a mac, but this was actually really irritating me when I was xterm’ing into one of my linux boxes from windows.

  • cygo

    “After perusing the man page for a while …”

    Maybe it’s a bit easier to scan man pages for essential information using:

    pman, http://snippets.dzone.com/posts/show/4274

    or

    openman, http://codesnippets.joyent.com/posts/show/1599

    Just a not-quite-on-topic tip!

    And thanks for sharing the export LESS=”-erX” hint!

  • http://kiss.souagil.com Sir Bode Rafael

    Hi!

    Thanks for this Post!

    I was experiencing this kind of trouble since I’ve started working on git!

    Best Brazilian Regards!

  • binOr

    Thank you so much! I was annoyed by “^M” ‘s at the end of the diff lines. Now they are gone!

  • Björn Skoglund

    Weird stuff, your post fixed my colors on OpenSuSE at work but strangely enough I have had no problems on my Leopard iMac at home.

  • http://looks-interesting.blogspot.com/ Giorgio Gonnella

    Thank you very much! I had exactly the same problem, now is solved. I wish this was documented in git tutorials, although is not directly git related…

  • http://techdebug.com Lantrix

    Looks like this is fixed under Snow Leopard. I get colours with my config set correctly in ~/.gitconfig and I _did not_ need to have the less switches in my ~/.profile

  • http://sites.google.com/site/rpg314/ Rohit Garg

    Hi,

    Thanks for your googling and your blog. I was having the same issues on opensuse 11.1.

    http://rpg-314.blogspot.com/2009/10/getting-colors-i-git-output-on-opensuse.html

    This post helped me fix it. Thanks a lot.

  • pablitostar

    I found using the -r flag did fix git-diff, but it broke something else in less. Specifically, searching in files using less. The token would be found, but some of the matching lines would be hidden. Not very useful, that. I changed the less flag from -r to -R and both git-diff and less search works. Here’s a snippet from the less man page, which is very apropos to git-diff:
    -R or –RAW-CONTROL-CHARS
    Like -r, but only ANSI “color” escape sequences are output in
    “raw” form. Unlike -r, the screen appearance is maintained cor-
    rectly in most cases. ANSI “color” escape sequences are
    sequences of the form:

    ESC [ … m

  • Girish KS

    Thanks for the post Nate and thanks pablitostar for your suggestion. I started using git few days back and was totally annoyed by ESC chars in diff !

  • Silly Avatar

    Thanx for this blog entry.
    Was having this problem while ssh-ing to a linux vps w/Putty. Thought it was a faulty terminal emulation causing the problem. Nope, just the goofy default LESS options in OpenSuSe on the vps. I’ve run linux boxen for years off and on. Never had so much problem with raw ESC character output til it was highlighted by git.

  • http://krishnaggk.wordpress.com Gopala Krishna A

    Thanks a lot!! This really proved helpful on opensuse 11.2 :)

  • http://www.vandeenensupport.com Bart van Deenen

    Thanks. Your post fixed my issues on OpenSuse 11.2.

  • Michal

    You are my hero for today!

  • Alexander

    Thanks a lot! Very helpful =)

  • fwip

    Thanks a ton. Also, pablitostar, you get my nomination for most helpful comment.

  • http://carlo-hamalainen.net Carlo

    Thanks, that did the trick on our Linux HPC.

  • Brian Cunnie

    Thanks, I was having the same problem under FreeBSD.

  • Ricardo Parada

    Thank you so much. For months I struggled to get colors to display correctly in Terminal when using git. I looked in all the wrong places and tried all kinds of things. And your post helped me fix this problem.

  • not-just-yeti

    Thanks, git diff and git show now work like a charm for me; in fact, they are positively beautiful in color. (I’d previously been kludging by using git diff --no-color ....)

    Reading less‘s man page also led me to its options --no-init [leave the file on-screen after finishing, rather than restore the terminal window to its pre-less state] and --quit-if-one-screen, both helpful in non-git contexts. (However, --quit-if-one-screen without --no-init is truly scary: it makes me think short files are entirely empty, since it immediately over-draws what it just displayed!)

    Oddly: If the environment variable $LESS is entirely unset, the defaults work well (as @Lantrix suggests). However, setting $LESS to anything at all (even the empty string) suddenly yields all-options-off rather than default-options. (I’m using OS X 10.6..8, and less --version reports “less 418″.)

  • Jon Erdman

    Thanks, fixed me on Ubuntu/Debian.

  • microft

    Thanks a lot. Worked on debian.

  • Oliver

    Solved it for me on Mageia as well! Thanks!

  • David

    Just another me-too thank you note, this time on RHEL (linux).

  • Jack

    Solve the problem for me. Thanks

  • Anonymous

    Thank you! Changed PAGER to less and problem solved.