Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git alias output coloring in function

I'm tring to add a complex git alias that will echo messages as it performs commands. I'd like to colorize some of the messages (Red for error, etc).

[alias]
    test = !"f() { echo "\033[31mHello\033[0m World"; }; f"

However when I execute the alias I get an error:

bad config line X in file .gitconfig`

Running the same command echo "\033[31mHello\033[0m World" in the terminal colorizes just fine.

like image 874
Jared Knipp Avatar asked Sep 19 '25 01:09

Jared Knipp


1 Answers

Backslashes have to be escaped. From the git-config docs...

Inside double quotes, double quote " and backslash \ characters must be escaped: use \" for " and \ for .

The following escape sequences (beside \" and \) are recognized: \n for newline character (NL), \t for horizontal tabulation (HT, TAB) and \b for backspace (BS). Other char escape sequences (including octal escape sequences) are invalid.

This will work.

test = !"f() { echo \"\\033[31mHello\\033[0m World\"; }; f"

But if your aliases are so complicated that you need to define functions that can turn into a big mess. I'd recommend putting those functions in their own file and sourcing it.

test = !"source ~/.gitfuncs; f"

$ cat ~/.gitfuncs
f() { echo "\033[31mHello\033[0m World"; };
like image 122
Schwern Avatar answered Sep 20 '25 17:09

Schwern