0

I'm trying to set the color of my git branch name and path in my terminal, but keep the rest of the text standard in the theme. The problem I have is that I was able to change the color of my git branch, but it also now changes the color of everything I'm typing in terminal to the same color as the git branch. I believe the problem is that in my prompt command the text isn't escaped correctly. How can I fix this?

# Find and set branch name var if in git repository.

function git_branch_name()
{
  branch=$(git symbolic-ref HEAD 2> /dev/null | awk 'BEGIN{FS="/"} {print $NF}')
  if [[ $branch == "" ]];
  then
    :
  else
    echo '- ('$branch')'
  fi
}

# Enable substitution in the prompt.
setopt prompt_subst

COLOR_DIR=$'\e[1;32m'
COLOR_GIT=$'\e[38;5;39m'

# Config for prompt. PS1 synonym.
prompt='%2/ ${COLOR_GIT}$(git_branch_name) > ' (I believe the problem is here)

enter image description here

torek
  • 448,244
  • 59
  • 642
  • 775
London804
  • 1,072
  • 1
  • 22
  • 47
  • Set the color back to whatever the standard color is. That's a zsh thing, not a Git thing, so I snipped the [tag:git] tag. – torek Dec 08 '21 at 00:16
  • So I changed it back to this "prompt='%2/ $(git_branch_name) > '" – London804 Dec 08 '21 at 00:18
  • That shows the git folder, but now it's white. I'm trying to customize my prompt, but it's not clear how to. I want the branch to be blue, the path to be green, and the text to be white. – London804 Dec 08 '21 at 00:19
  • I don't know what the zsh variables for these are, but in general, you print ESC [ m, and then after that, text shows up in colors, or other effects. Then you print ESC [ m, and after that, text shows up in different colors or other different effects. Repeat as desired. Set foreground and/or background to whatever you like each time. Use ESC [ m (with no numbers) to clear all settings. – torek Dec 08 '21 at 00:43
  • Each of those `'\e[1;32m'` etc are the "make future characters be in particular colors" sequences. You can look up these ANSI color sequences: https://www.google.com/search?q=ansi+color+escape+codes (and see also [here](https://stackoverflow.com/q/4842424/1256452)). – torek Dec 08 '21 at 00:45
  • How do I include this in my prompt so it's read correctly? This doesn't work prompt='%2/ $(COLOR_GIT)$(git_branch_name) > ' – London804 Dec 17 '21 at 15:53
  • That's a zsh question, which I can't answer. In bash, there are no pre-defined color variables, so you have to set them yourself. – torek Dec 17 '21 at 18:53

0 Answers0