How to get rid of ESC[ characters when using git diff on Mac OS X Mavericks?
Categories:
Taming ANSI Escape Codes in Git Diff on macOS Mavericks

Learn how to eliminate distracting ANSI escape characters (ESC[) from your git diff
output on macOS Mavericks for cleaner, more readable code comparisons.
When working with Git on macOS Mavericks, you might occasionally encounter strange ESC[
characters interspersed within your git diff
output. These are ANSI escape codes, which are normally interpreted by your terminal to produce colored text. However, sometimes they can appear raw, making the diff output difficult to read and parse. This article will guide you through understanding why this happens and provide effective solutions to clean up your git diff
.
Understanding ANSI Escape Codes in Git Diff
Git uses ANSI escape codes to provide syntax highlighting and colorization in its output, especially for git diff
. This feature is incredibly useful for quickly identifying changes. The problem arises when your terminal emulator, or the environment in which Git is running, fails to interpret these codes correctly. Instead of rendering colors, it displays the raw escape sequences, which typically start with ESC[
(often represented as [
or ^[
). This can happen due to various reasons, including terminal misconfiguration, piping output to a program that doesn't handle ANSI codes, or specific environment variables.
flowchart TD A[Git Diff Command] --> B{Terminal Environment} B -->|Interprets ANSI| C[Colored Output] B -->|Fails to Interpret ANSI| D[Raw ESC[ Characters] D --> E[Unreadable Diff]
Flowchart illustrating how ANSI escape codes are processed (or not processed) by the terminal.
Common Causes and Solutions
The most frequent reason for seeing raw escape codes is when Git detects that its output is not going directly to an interactive terminal. For instance, if you pipe git diff
to less
, grep
, or redirect it to a file, Git might automatically disable color output to avoid embedding uninterpretable codes in the non-terminal stream. However, sometimes this auto-detection can be overridden or misconfigured, leading to the issue. The primary solution involves explicitly telling Git how to handle color, or using tools that can strip these codes.
git config --global color.ui auto
Set Git's color output to 'auto', which is usually the default and recommended setting.
color.ui
setting can be always
, auto
, or never
. auto
is generally the best choice as it enables color when outputting to a terminal and disables it when piping to a file or another command.Stripping ANSI Codes for Cleaner Output
If adjusting Git's color settings doesn't fully resolve the issue, or if you specifically need to process the diff output without any ANSI codes (e.g., for scripting or logging), you can use external tools to strip them. A common and effective tool for this is sed
or ansi2txt
.
git diff | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
Use sed
to remove common ANSI escape sequences from git diff
output.
This sed
command uses a regular expression to match and remove most common ANSI escape codes. The \x1B
represents the escape character, and \[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]
matches the subsequent code for color or cursor movement. The -r
flag enables extended regular expressions, which makes the pattern slightly more readable.
ansi2txt
or colordiff
(which can also be configured to strip colors if needed).1. Check Git Color Configuration
First, verify your global Git color settings. Open your terminal and run: git config --global color.ui
. If it's not auto
, consider setting it to auto
or never
if you consistently want plain text output.
2. Apply sed
for On-the-Fly Stripping
If you need to view a specific diff without colors, pipe the git diff
command through sed
as shown above: git diff | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
. This provides a clean, uncolored output for that particular command.
3. Consider Aliasing for Convenience
To make this easier, you can create a Git alias or a shell alias. For example, add alias gdnc='git diff | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
to your ~/.bash_profile
or ~/.zshrc
to quickly use gdnc
for a 'git diff no color'.