function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%{%F{245}%}%n %{%F{normal}%}%2~ %{%F{green}%}$(parse_git_branch) %{%F{normal}%}$%{%f%} '
reference: link
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%{%F{245}%}%n %{%F{normal}%}%2~ %{%F{green}%}$(parse_git_branch) %{%F{normal}%}$%{%f%} '
reference: link
Also, for the sake of demonstrating the history:
You'll run into weird issues of lines wrapping and messing up unexpectedly.
This is a fix for that:
export PROMPT='%{%F{243}%}%n %{%F{197}%}%~ %{%F{39}%}$(parse_git_branch) %{%F{normal}%}$%{%f%} '
Note the use of %{...%}
to make sure that formatting characters aren't being counted in the character count of the line. If someone comes across this and finds a better way to do it, please definitely improve this.
I found this stackover flow helpful to start to see what was up: https://stackoverflow.com/questions/689765/how-can-i-change-the-color-of-my-prompt-in-zsh-different-from-normal-text
Thanks @neiloler - This fix was very helpful and the tips re color was a good add-on. It's easy to choose colors of choice for username, directory path, git branch etc in that order
'${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
Thanks for your feedback! @neiloler
I applied your feedback.
Thank you so much! @neiloler
Thank you!
Thank you for this!
Thank you so much!
Thank you!! @neiloler . I got inspired by your post and tweaked it a bit.
https://gist.github.com/jsohndata/7d5a252de861c2fe3c1eb4c4143be02d
Some how I missed all the notifications for the kind thanks here, thanks! 🙂
For anyone who comes here looking for the fix this bit of code offers, but wants to play around with colors, but isn't a ZSH-pro like I am (or wasn't before I went on this kick to find out about colors...). I used the colors from the reference link, because I liked them. 😅
The ZSH feature of
%F{...} thingToBeColored %f
is handy, but it was tough to break it down to understand what was going on. It takes a color name, such asnormal
orgreen
, but it also takes an ANSI color code...number? Not sure what it's called, butANSI color code of
red
is\u001b[31m
, but the31
is what you'd pass into the&F{31}IamCool%f
to makeIamCool
red.This might be a "no duh" thing for others, but I figured I'd leave a little bit of what I found.