2

Run this in your Bash terminal:

f="\e[;1;4;5;94m"   # beginning ANSI format string
F="\e[m"            # ending ANSI format string
echo -e "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

You'll see the string formatted bold, underlined, blinking, and bright blue, per the ANSI escape codes I put in it.

I'd like to get formatting text like that into my git log messages. How can I do that?

My current attempts, such as this, just put in the raw chars instead of doing the formatting:

# doesn't work
git commit -m "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

I also tried pasting the output from the first echo above, both with and without the escaped codes manually in it as chars, into my git editor (currently Sublime Text, as I show how to set here), and that didn't work either.

Note that the very existence of this question seems to indicate that what I am trying to do is at least possible: ANSI color in git is not displayed correctly--unless I'm misunderstanding and hey are just talking about git grep showing git-controlled files. Update: I think I'm misunderstanding: I think they are talking about the ANSI escape codes in git log, such as the coloring and bold of the commit hashes themselves.

Why do I want to do this?

I think it would be neat to make some of my custom commit messages in my git log output blink and be blue bold or something, to make certain really special commits stand out. I just wrote a Bash library (ansi_text_format_lib.sh) to allow easy text formatting and thought I'd use it at the command-line to do this.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • `git log` has its own formatting specifiers. Look for `%C` in the manual ([1](https://git-scm.com/docs/git-log#Documentation/git-log.txt-emCredem) [2](https://git-scm.com/docs/git-config#Documentation/git-config.txt-color)). – John Kugelman Feb 18 '23 at 19:29
  • I imagine it does this so it can selectively enable/disable color codes based on where the output is going. It might also be so it can keep track of the cursor position and thus where to insert line wraps. ANSI codes make this difficult because they don't move the cursor a predictable amount. That's why `less` requires `-r` or `-R` to show colors; you're basically telling `less` that you want to see the escape codes and you're okay if the display gets a bit messed up. – John Kugelman Feb 18 '23 at 19:33
  • 1
    For example, my go-to is `log --graph --abbrev-commit --date=relative --format=format:'%C(bold blue)%aN <%aE>%C(reset), %C(bold green)%ad%C(reset) - %C(auto)%h%d%C(reset)%n%C(bold)%B%-C()%n' --stat`. – John Kugelman Feb 18 '23 at 19:38
  • Thanks @JohnKugelman. I like that `git log` output of yours. Keep that there for the world to see and for me to reference later. What I'm trying to do though is get formatted output into one or two lines in *some* of my custom commit messages in my `git log` output. I'm not trying to generically format the whole git log output. You know, like I scroll down in my output and _one_ of my commit messages has a bold blue blinking line that pops out to me because that commit had something really special I wanted to remember. – Gabriel Staples Feb 18 '23 at 19:47
  • I'm going to guess that's not possible. Are you willing to run a special `log` command that disables processing and shows raw characters? I don't know if that exists but it might. And of course other people will see ugly escape sequences. – John Kugelman Feb 18 '23 at 19:48
  • @JohnKugelman, yeah, it may not be possible, and no, I don't necessarily want to make a special `git log`function just for me. – Gabriel Staples Feb 18 '23 at 19:52

1 Answers1

0
git commit -m "${f}This string is bold, underlined, blinking, bright blue.${F} This is not."

What's happening there is, you're putting escape sequences for the ansi color control sequences into the commit message.

What you want is to put the ansi color control sequences themselves into the commit message. Easiest way I know to get that during variable substitution is to use prompt-escape expansion:

# (First set your variables like you already do):
f='\e[;1;4;5;94m'
F='\e[m'

# then
git commit -m "${f@P}This string is bold, underlined, blinking, bright blue.${F@P} This is not."

Run man bash and hunt up parameter transformation and prompting to see what else you can do with that.

jthill
  • 55,082
  • 5
  • 77
  • 137