Skip to content

Instantly share code, notes, and snippets.

@kkdeok
Last active December 20, 2022 20:41
Show Gist options
  • Save kkdeok/c4fcd2e65c3397947248878065b644ed to your computer and use it in GitHub Desktop.
Save kkdeok/c4fcd2e65c3397947248878065b644ed to your computer and use it in GitHub Desktop.
Git Branch Terminal Prompt for zsh (MacOS Catalina)

Git Branch Terminal Prompt for zsh (MacOS Catalina)

sample image

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

@neiloler
Copy link

neiloler commented Aug 4, 2021

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. 😅

export PROMPT='%F{243}%n%f %F{197}%~%f %F{39}$(parse_git_branch)%f %F{normal}$%f '

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 as normal or green, but it also takes an ANSI color code...number? Not sure what it's called, but

ANSI color code of red is \u001b[31m, but the 31 is what you'd pass into the &F{31}IamCool%f to make IamCool red.

This might be a "no duh" thing for others, but I figured I'd leave a little bit of what I found.

@neiloler
Copy link

neiloler commented Aug 4, 2021

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

@juvestig
Copy link

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} $ '

@kkdeok
Copy link
Author

kkdeok commented Jan 24, 2022

Thanks for your feedback! @neiloler
I applied your feedback.

@DandelionLove
Copy link

Thank you so much! @neiloler

@MadaShindeInai
Copy link

MadaShindeInai commented May 22, 2022

Thank you!

@kevin-lin
Copy link

Thank you for this!

@ArtemyKv
Copy link

ArtemyKv commented Jul 8, 2022

Thank you so much!

@jsohndata
Copy link

jsohndata commented Dec 10, 2022

Thank you!! @neiloler . I got inspired by your post and tweaked it a bit.
https://gist.github.com/jsohndata/7d5a252de861c2fe3c1eb4c4143be02d

@neiloler
Copy link

Some how I missed all the notifications for the kind thanks here, thanks! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment