Marc Fletcher
Marc Fletcher

Reputation: 1082

zsh prompt not evaluating colors correctly

My .zshrc:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

cNone='\033[00m'
cRed='\033[01;31m'
cGreen='\033[01;32m'
cYellow='\033[01;33m'
cPurple='\033[01;35m'
cCyan='\033[01;36m'
cWhite='\033[01;37m'
cBold='\033[1m'
cUnderline='\033[4m'

COLOR="\033[32m\]"
export PS1="(parse_git_branch) %~ ${cRed}♥ "

Output:

(parse_git_branch) ~/someDirectory/Another \033[01;32m♥

Not sure why it isn't escaping the colors correctly nor why it isn't evaluating the git branch command.

Upvotes: 0

Views: 3011

Answers (1)

Kevin Cui
Kevin Cui

Reputation: 846

Firstly, according to http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html, zsh supports using %F to start font color and %f to stop font color:

Start (stop) using a different foreground colour, if supported by the terminal. The colour may be specified two ways: either as a numeric argument, as normal, or by a sequence in braces following the %F, for example %F{red}.

To make a red ♥: %F{red}♥%f

Secondly, to fetch the git branch name, there is an easier way:

git rev-parse --abbrev-ref HEAD 2> /dev/null

Thirdly, in order to run command inside prompt setopt prompt_subst is required

Finally, here is your colorful prompt: setopt prompt_subst PS1='$(git rev-parse --abbrev-ref HEAD 2> /dev/null) %~ %F{red}♥%f '

If you want to use " (double quotes) instead of ' (single quotes), a \ (backslash) is needed before $: setopt prompt_subst PS1="\$(git rev-parse --abbrev-ref HEAD 2> /dev/null) %~ %F{red}♥%f "

More info can be found here https://stackoverflow.com/a/36196179/8272771

Upvotes: 3

Related Questions