Closed paulej closed 6 years ago
I also like to have a space in my prompt. I hard-coded it.
This is a neat solution to what I can only imagine is a common problem.
+1 Nice suggestion. I was going down the path of creating git_space as a separate function using the output of this:
git rev-parse --git-dir > /dev/null 2>&1
but I think your solution is just as effective (assuming everyone uses git_branch). I also like your idea of having separate git_branch_space and git_dirty_space for robustness. PR would be awesome.
This is the git-related part of my prompt, which adds a space before the branch name. Does this work for you?
${git_branch:+ \[$txtgrn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]}
What I did rather than modifying the code in the repository, I just used PROMPT_COMMAND to do that for me. I set this in .bashrc:
PROMPT_COMMAND='if [ -z "$git_branch" ]; then git_space=""; else git_space=" "; fi'
This effectively does what I want. I still use largely the same PS1 setting, with just a color change:
PS1='[\u@\h \w\['\$txtgrn'\]'\$git_space\$git_branch'\['\$txtred'\]'\$git_dirty'\['\$txtrst'\]]\$ '
I changed the PR to just a README recommendation explaining lethosor's response to use shell variable expansion in the PS1 prompt instead of having explicit space variables in the code. Thanks!
I think @jonathandandries PR documenting @lethosor proposal works great. I'll close this.
I just found this repo. Very nice! Much appreciated!
Normally, I have this for a prompt:
Using what was provided here, I created this prompt:
However, I'd prefer to have a space before the name of the git branch. If I insert one into PS1, I always have a space after the normal closing bracket and the $.
So what I did was this:
The git_space is an empty string when git_branch is empty and contains one space when git_branch is non-empty. I did this by first setting PROMPT_COMMAND to my own script that will inspect $git_branch and assign $git_space appropriately. It works, but I'd prefer to see if I could have something added to the repo so I don't have to do that as an extension.
Would you entertain this change, just adding two git_space assignments?
One could then insert a space before and/or after the repo name as I have done in my PS1 example above. Some might want the same kind of thing between the branch name and $git_dirty. So perhaps we might have $git_branch_space and $git_dirty_space.
I'd be happy to do a PR is this would be of interest.