Martin Vegter
Martin Vegter

Reputation: 527

How to use ANSI 256-color in .gitconfig

I am trying to set up custom colors for git status, as described here

While using predefined color names works:

[color "status"]
    changed = yellow normal

using ANSI 256 color codes does not work:

[color "status"]
    changed = "\033[38;5;214m" normal

I get an error:

fatal: bad config line 31 in file .gitconfig

I have tries using hex code, but that does not work either:

[color "status"]
    changed = "#d70000" normal

I suspect the # is interpreted as comment.

My git version is 2.20.1

Upvotes: 2

Views: 605

Answers (3)

bk2204
bk2204

Reputation: 76489

If you want to use a 256-color code, you can do so with a normal number from 0 to 255. For example, in my .gitconfig, there's these entries:

[color.diff]
        new = 34
        old = 203

You don't want to place an actual escape sequence into the file. Git knows how to take a numeric value and issue the proper ANSI escape sequnce. It does not, however, handle terminal-specific capabilities or use terminfo, so if your terminal doesn't support the standard ANSI sequences, then it won't work with Git.

Upvotes: 4

Giacomo Catenazzi
Giacomo Catenazzi

Reputation: 9523

From documentation (https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration):

You can set the color to any of the following values: normal, black, red, green, yellow, blue, magenta, cyan, or white. If you want an attribute like bold in the previous example, you can choose from bold, dim, ul (underline), blink, and reverse (swap foreground and background).

So you have just a limited selection, OTOH it is good to have few colours on terminals (you can change each colour, e.g. solarize the palette, but for usability, less is better).

Note: using names is good: git will find and "outsource" the generation of correct escape sequence according to the terminal of choice.

Upvotes: -3

VonC
VonC

Reputation: 1324268

First, "" would not be supported in config

changed = #d70000 normal

Second, your terminal has to support it. In a CMD on Windows, for instance, that would not work.

I did not manage to include Ansi escape code though.

Upvotes: 2

Related Questions