3

I'm basically trying to create a PS1 that has this output:

$ ~/Projects/Blah (master):

However, if the folder I'm in is not a Git repository, I want it to look like this instead:

$ ~/Projects/Blah:

This is my current PS1:

export PS1="$ \w \$(__git_ps1): "

It gives me the output I want for when I'm in a Git repo, but when I'm in a folder that is not in a Git repo, the output looks like this:

$ ~/Projects/Blah :

I don't really want that space there if it isn't a Git repo.

Is there some way I can specify this in my PS1?

Jahid
  • 21,542
  • 10
  • 90
  • 108
Saad
  • 49,729
  • 21
  • 73
  • 112
  • 2
    Do you control the output from `__git_ps1` just move the space over there . – Andreas Louv May 04 '16 at 08:17
  • I have `__git_ps1` equal to `"git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"`. I tried inserting spaces in various places there, but it didn't seem to work. I'm not too familiar with what's going on with that line, I believe I copied it from somewhere. – Saad May 04 '16 at 08:22

4 Answers4

4

It's usually much simpler to build your prompt up piece-by-piece using PROMPT_COMMAND, which is code executed just before each prompt is displayed. (PS1 doesn't need to be exported, by the way.)

build_prompt () {
    PS1="$ \w"
    git_info=$(__git_ps1)
    if [[ $git_info ]]; then
        PS1+=" $git_info"
    fi
    PS1+=": "
}
PROMPT_COMMAND=build_prompt
chepner
  • 497,756
  • 71
  • 530
  • 681
2

I ended up using this .git-prompt.sh file. Steps to get this to work:

  1. Create a file called .git-prompt.sh in your home directory (~/.git-prompt.sh) and copy the code from the link above into it.
  2. In your .bash_profile or .bashrc file, add this line: source ~/.git-prompt.sh
  3. Change your PS1 to this: PS1='\n$ \w$(__git_ps1 " (%s)"): '
Saad
  • 49,729
  • 21
  • 73
  • 112
0
export PS1="$ \w \$(__git_ps1): "

I have __git_ps1 equal to git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'.

Change your __git_ps1 to:

git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/ (\1)/'

Note the extra white-space in the sed replacement before (\1).

Edit1: Simplified __git_ps1 by @andlrc with one command less:

__git_ps1() { git branch 2>/dev/null | sed -n 's/\* \(.*\)/ (\1)/p'; }
Community
  • 1
  • 1
Dummy00001
  • 16,630
  • 5
  • 41
  • 63
0

Prepare the git_ps1 like this:

git_ps1=$(git branch 2>/dev/null | grep '*')
git_ps1="${git_ps1:+ (${git_ps1/#\* /}) }"
##                  ^space             ^space
## Change the spaces if you like it other ways.

git_ps1 will be like (master), with spaces at both ends; and a complete empty string when git_ps1 is not set (for non-git dir) . Now you can just use the $git_ps1 variable to insert it anywhere you like.

Explanation:

git_ps1="${git_ps1:+ (${git_ps1/#\* /}) }"

is a conditional variable assignment.

  1. if git_ps1 is set then it will be equal to (${git_ps1/#\* /}), otherwise it will remain empty.

  2. ${git_ps1/#\* /} cuts the * and space from the beginning of $git_ps1


Example Usage:
__git_ps1(){
    git_ps1=$(git branch 2>/dev/null | grep '*')
    git_ps1="${git_ps1:+ (${git_ps1/#\* /})}"
    echo "$git_ps1"
}
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[00m\]\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1) \[\033[1;32m\]>\[\033[00m\]\[\033[1;32m\]>\[\033[00m\]\[\033[1;32m\]>\[\033[00m\] '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This gives me a prompt like this one: enter image description here

Jahid
  • 21,542
  • 10
  • 90
  • 108