4

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.

Jared Knipp
  • 5,880
  • 7
  • 44
  • 52

1 Answers1

5

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"; };
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • I was close, I must have been missing one of the escapes. I plan to pull it out once I finish working on it - thanks for the info though! – Jared Knipp Feb 22 '17 at 23:13