jimeh / git-aware-prompt

Display current Git branch name in your terminal prompt when in a Git working directory.
Creative Commons Zero v1.0 Universal
2.15k stars 340 forks source link

Issue in mac command #40

Open awebdeveloper opened 8 years ago

awebdeveloper commented 8 years ago
Mac OS X

export PS1="\u@\h \W \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "

Sq brackets are missing around $git_branch and $git_dirty

\[$txtcyn\]\[$git_branch\]\[$txtred\]\[$git_dirty\]\[$txtrst\]\$

joeytwiddle commented 8 years ago

Square brackets are needed for non-printing characters (the colour codes). They will prevent the terminal from counting those characters when calculating the number of columns.

For characters which are displayed, the terminal needs to count them, so I don't think they should be in square brackets.

Was there any bug you experienced related to this?

Veloxious commented 5 years ago

Super late to the thread. I was also having an issue where the variable $git_branch was being displayed as text (e.g. $git_branch) instead of the actual branch name.

awebdeveloper's solve made it work.

lethosor commented 5 years ago

Can you post the prompt(s) that worked and didn't work for you? Putting \[ and \] around printable text like $git_branch is simply not correct.

Veloxious commented 5 years ago

Sure,

My current working ps1 export export PS1='\e[0;32m\t\e[m \e[0;34m\u\e[m \w \e[0;35m\[$git_branch\]\e[m\nλ: '

and the previous export (that was not working but now is, likely user error) export PS1='\e[0;32m\t\e[m \e[0;34m\u\e[m \w \e[0;35m$git_branch\e[m\nλ: '

as I said, now both of these exports work correctly. when I set this up I must have forgotten to source or restart my session.

joeytwiddle commented 5 years ago

It looks like the confusion has arisen from using ' instead of " for the prompt string.

If you use " then \$ is needed. If you use ' then $ is needed.

But in either case, square brackets are not desirable around visible text.

\[$git_branch\] should just be $git_branch