The Stupid Engineer
The Stupid Engineer

Reputation: 402

How to preserve colours rendered by git_ps1 with a multiline prompt?

I have the following .bash_profile

#Change alias for ls to include colours
alias ls='ls -Gh'

#Enable git branch completion
source ~/git-completion.bash

#Allows git information to be visible in prompt
source ~/git-prompt.sh

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWCOLORHINTS=1

# ANSI colors: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_GREY="\[\033[0;37m\]"
DARK_GREY="\[\033[1;30m\]"
NO_COLOUR="\[\033[0m\]"

##################################
#Configure multiline prompt
# Prompt appearance should be:
#[RED]user@host[\RED] [CYAN]working_directory[\CYAN] git_branch [COLOUR_FROM_GIT]asterix_indicator[COLOUR_FROM_GIT] ==>
#==>
##################################
#This works for a multiline prompt and no colours
PS1='\[\033[0;31m\]\u@\h\\[\033[0m\] \[\033[0;36m\]\w\[\033[0m\] $(__git_ps1 "(%s)")==> \n==>'

According to the git-prompt.sh file, I should see colours when setting the GIT_PS1_SHOWDIRTYSTATE and GIT_PS1_SHOWCOLORHINTS flags. I should see a red asterisk if there are any uncommitted changes. I only see a green asterisk.

I was able to get the red asterisk and saved my bash_profile once this occurred. However, when opening a new terminal my changes were gone.

Any ideas why:

I am using OSX Sierra.

Thanks!

EDIT: I've gotten close to where I want using: PROMPT_COMMAND='__git_ps1 "\[\033[0;31m\]\u@\h\[\033[0m\]:\[\033[0;36m\]\w\[\033[0m\]" "\\\$ "' I've temporarily given up on using variable names for colours. However, if I try to insert a newline into this e.g: PROMPT_COMMAND='__git_ps1 "[\033[0;31m]\u@\h[\033[0m]:[\033[0;36m]\w[\033[0m]" "\\$ \n$"' and substitute '$' with '==>' then it breaks the git branch information. Any advice?

Upvotes: 1

Views: 970

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149796

According to the git-prompt.sh file, I should see colours when setting the GIT_PS1_SHOWDIRTYSTATE and GIT_PS1_SHOWCOLORHINTS flags.

In that very file you can read (emphasis mine):

The colors are based on the colored output of "git status -sb" and are available only when using __git_ps1 for PROMPT_COMMAND or precmd.

So in order to see the colored hint, you need to use __git_ps1 for PROMPT_COMMAND. There's an example of how to do this at the top of the script:

PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'

Upvotes: 3

Related Questions