Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI color in git is not displayed correctly

Tags:

git

bash

Recently, I switched to SLES 11. I found a problem for git command. All the ANSI color could not be rendered. Instead, it shows the ANSI code like this:

*ESC[33m*commit 0a02124a0fd85c1f0094bcdf15bac1645e3c8630ESC[m

note: the ansi color in 'ls' works very well.

like image 961
Rocky Avatar asked Dec 13 '11 04:12

Rocky


People also ask

What is ANSI color in terminal?

The ANSI ASCII standard represents the escape ESC character by the decimal number 27 (33 in octal, 1B in hexadecimal). This is one of the control characters (0-31 and 127), not one of the printable characters (32-126).


2 Answers

Try setting your LESS environment variable to include the -R option:

LESS=-R git grep ... 

If this works, add export LESS=-R to your ~/.bashrc or ~/.profile or similar shell startup file.

   -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 
like image 196
sarnold Avatar answered Sep 21 '22 23:09

sarnold


The problem, as others have noted, is that your terminal is fine, but when Git invokes the pager, it's not interpreting the ANSI color codes correctly.

I'd start out by unsetting LESS in your environment; it sounds like you might have previously been setting it to something obscuring what Git needs. If that solves it, there you are. If you really must customize LESS, note that Git starts out with FRSX as default, so be wary of changing those if you don't need to.

If you do for whatever reason want LESS in your environment different from what you want for Git, the ideal way to deal with Git and the pager is through the core.pager config variable. To quote the manpage:

The command that git will use to paginate output. Can be overridden with the GIT_PAGER environment variable. Note that git sets the LESS environment variable to FRSX if it is unset when it runs the pager. One can change these settings by setting the LESS variable to some other value. Alternately, these settings can be overridden on a project or global basis by setting the core.pager option. Setting core.pager has no affect on the LESS environment variable behaviour above, so if you want to override git’s default settings this way, you need to be explicit. For example, to disable the S option in a backward compatible manner, set core.pager to less -+$LESS -FRX. This will be passed to the shell by git, which will translate the final command to LESS=FRSX less -+FRSX -FRX.

That, combined with some knowledge of the options you want, should get you where you want to be. (The fancy backward-compatible method works by disabling all options currently in LESS, then adding back in the ones you want.)

like image 39
Cascabel Avatar answered Sep 18 '22 23:09

Cascabel