0

tl;dr: I want my command prompt to be customizable with any color I want and also display the current git branch that I am on.

I am aware that there are at least six different post asking an extremely similar question. I have read the ones I have found and I still have my command prompt cycling and having problem even with their suggestion when I insert colors. There are a few details that make my question different and worthwhile I hope.

Currently this is what my PS1 variable looks like:

PS1='\[\e[0;31m\u \W $(__git_ps1 "(%s)")\$ \e[m\]'

As the other posts suggested, I inserted the:

\[...\]

I am aware that:

ctrl + L 

clears the screen but that does not really address the problem that is, that the command prompt should not be acting weird.

One specific problem that it has is that, after I type something and then delete it, the command prompt disappears all together. When I try to look at my command history it also overwrites my command prompt with the history. I believe commands are executed correctly but I am not sure why it behaves weirdly.

Also, the __git_ps1 is a function in a shell script for git that I downloaded at:

https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh

I am not sure if that function is the problem or if its the different type of quotes. I have tried different quotes. I have also tried storing the value of what __git_ps1 returns and then putting it in the string. I think I tried it correctly, but if anyone has something that works, I would be very grateful!

Also, I am using bash and a MAC with Mavericks. Just in case this is helpful.

1 Answer 1

1

Try putting the \[...\] brackets just around the color escape sequences. Those brackets tell bash that the contents are 0 characters long when displayed so if you put actual characters inside then bash will think your line is shorter than it actually is.

To address your comments (and to improve this answer), the working prompt:

'\[\e[0;31m\]\u \W $(__git_ps1 "(%s)")\$ \[\e[m\]'

differs from your original by use of the escaped brackets \[ and \]. The sequences inbetween these brackets e.g. \e[0;31m is an escape sequence that changes the color of the text in your prompt and \e[m is an escape sequence that returns the color to the default color. These sequences do not write actual characters to the terminal so you need to tell bash not to count them toward the length of the prompt; the brackets are how to communicate this to bash.

In your original prompt, you had the brackets around the entire prompt, and this will tell bash your prompt is 0 bytes long, and so your prompt will get overwritten under certain circumstances that you discovered leading up to your post. If you had no brackets you would notice the opposite and things would appear a few characters past the prompt instead of right at it.

My comment about the git prompt was just to ensure that the length of the prompt was being tracked properly. If the color isnt changing, chances are you are fine. A rule of thumb for writing your prompt is to put the color codes (the \e...m sequences) inside the brackets and nothing else.

5
  • As in: PS1='\[\e[0;31m\]\u \W $(__git_ps1 "(%s)")\$ \[\e[m\]' ? Dec 31, 2013 at 18:56
  • Yes. If the git prompt has color codes you'll want to properly bracket its contents as well.
    – casey
    Dec 31, 2013 at 18:59
  • Well, it worked! @casey Do you mind explaining why it worked? how was it different from what I was doing? (also, sorry I cant upvote your answer, I don't have enough reputation yet, when I do, I'll come back and upvote it) Dec 31, 2013 at 19:04
  • Also, what do you mean by your second suggestion about the color codes for git? I don't think I have those because I don't even know what your question means. Dec 31, 2013 at 19:07
  • Updated answer.
    – casey
    Dec 31, 2013 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.