How to show git status and conda environment in command prompt?

2

1

I am using the git-prompt.sh script to show the git status at the bash command prompt. Using the PROMPT_COMMAND method to also have colors:

.bashrc :

source ~/.bash.d/git-prompt.sh # Show git branch name at command prompt
export GIT_PS1_SHOWCOLORHINTS=true 

# use existing PS1 settings
PROMPT_COMMAND=$(sed -r 's|^(.+)(\\\$\s*)$|__git_ps1 "\1" "\2"|' <<< $PS1)

This works so far in regard of showing the git status, but it does not show the conda environment anymore.

When I comment out the line with PROMPT_COMMAND=..., the conda env is displayed, e.g.

(base) [user@linux ~]$

Apparently the problem is that git-prompt.sh uses PROMPT_COMMAND and conda uses PS1.

Is there a way to achieve this? Maybe showing the conda env with PROMPT_COMMAND?

jost21

Posted 2019-08-08T15:25:14.997

Reputation: 195

Answers

2

You can just configure your PS1 to show the git branch by adding a function right above your PS1 declaration:

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

Then, you simply place \$(git_branch) wherever you'd like in your PS1, and colorize it as usual.

If you need more details, this blog shows you how.

Jaya

Posted 2019-08-08T15:25:14.997

Reputation: 21

It works and does the show the git branch. I think it should be $(git_branch) though (without the \). But it doesn't provide all the features you get with git-prompt.sh and PROMPT_COMMAND – jost21 – 2019-08-13T07:31:13.310