How to get rid of ESC[ characters when using git diff on Mac OS X Mavericks?

MacosGit

Macos Problem Overview


Since I installed OS X Mavericks, the result of the command git diff is rendering ESC[xxx characters like this:

ESC[1mdiff --git a/console/org.linkedin.glu.console-webapp/decorate-grails-methods-plugin/plugin.xml b/console/org.linkedin.glu.console-webapp/decorate-grails-methods-plugin/plugin.xmlESC[m
ESC[1mindex 3b65cf1..0ca5d7e 100644ESC[m
ESC[1m--- a/console/org.linkedin.glu.console-webapp/decorate-grails-methods-plugin/plugin.xmlESC[m
ESC[1m+++ b/console/org.linkedin.glu.console-webapp/decorate-grails-methods-plugin/plugin.xmlESC[m
ESC[36m@@ -15,14 +15,14 @@ESC[m ESC[mThe purpose of this plugin is to decorate various grails methods.ESC[m

This used to render properly prior to installing Mavericks. I have no clue what is different so any help in troubleshooting would be much appreciated.

Note that the pager used is less since when I hit h I get the following:

             SUMMARY OF LESS COMMANDS

Commands marked with * may be preceded by a number, N.
Notes in parentheses indicate the behavior if N is given.

Macos Solutions


Solution 1 - Macos

Do you have a LESS environment variable set? You can check using:

env | grep LESS

or:

echo $LESS

If so, you want to make sure the R option is in there. That allows the ANSI escape sequences to pass through unscathed, and they'll be rendered as colors in the terminal.

Just for reference, I use this:

export LESS=eFRX

Solution 2 - Macos

This works:

git config --global core.pager "less -r"

Solution 3 - Macos

Try "less -R" (big "R")

This worked for me:

git config core.pager 'less -R'

Example:

Without "-R" you just get raw ESC control codes: 1.Raw-Control-Codes

With "-R" you get the actual color: 2.color-codes-rendered

"less -R" vs. "less -r"

-R (big R) seems to be the safer version of -r (small r). So that's why I prefer the big R version.

Quote from man less: >-r or --raw-control-chars
>Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as "^A". Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, various display problems may result, such as long lines being split in the wrong place.

>-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 correctly in most cases. ANSI "color" escape sequences are sequences of the form: > >ESC [ ... m > >where the "..." is zero or more color specification characters For the purpose of keeping track of screen appearance, ANSI color escape sequences are assumed to not move the cursor. You can make less think that characters other than "m" can end ANSI color escape sequences by setting the environment variable LESSANSIENDCHARS to the list of characters which can end a color escape sequence. And you can make less think that characters other than the standard ones may appear between the ESC and the m by setting the environment variable LESSANSIMIDCHARS to the list of characters which can appear.

Solution 4 - Macos

The global pager configuration option within git, just sends the output stream to the more or less commands. You can get rid of the escape characters within this output by setting the global configuration option to either:

git config --global core.pager "more -R"

To continue using more as your pager or

git config --global core.pager "less -R"

To continue using less as your pager

Solution 5 - Macos

You change use the option -R Repaint the screen to remove all the buffered input.

export LESS="$LESS -R"

Solution 6 - Macos

Another alternative is to eliminate git's "colorizing" of output by doing something like

$ git config --global color.ui false

See git help config for details.

Reference: unix.stackexchange: How to colorize output of git?

Solution 7 - Macos

I had faced similar issues. My Output for LESS was: LESS=-iMPM--?f%f--:--.?lb lines %lt-%lb.?L of %L.?pB (%pB%). LESSOPEN=||/usr/bin/lesspipe.sh %s

Tried executing below, but it did not resolve the issue- git config --global color.ui auto true

The issue was resolved using - export LESS=eFRX

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyanView Question on Stackoverflow
Solution 1 - MacosJohn SzakmeisterView Answer on Stackoverflow
Solution 2 - MacosFreshPowView Answer on Stackoverflow
Solution 3 - MacosStackzOfZtuffView Answer on Stackoverflow
Solution 4 - MacosSri Murthy UpadhyayulaView Answer on Stackoverflow
Solution 5 - MacosskrView Answer on Stackoverflow
Solution 6 - MacosjhfrontzView Answer on Stackoverflow
Solution 7 - Macoschitra tolaniView Answer on Stackoverflow