git-aware-prompt icon indicating copy to clipboard operation
git-aware-prompt copied to clipboard

Issue in mac command

Open awebdeveloper opened this issue 7 years ago • 5 comments

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\]\$

awebdeveloper avatar Aug 08 '16 07:08 awebdeveloper

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?

joeytwiddle avatar Aug 12 '16 07:08 joeytwiddle

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.

Veloxious avatar Nov 26 '18 21:11 Veloxious

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.

lethosor avatar Nov 26 '18 21:11 lethosor

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.

Veloxious avatar Dec 09 '18 17:12 Veloxious

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

joeytwiddle avatar Dec 23 '18 19:12 joeytwiddle