Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Git Branch Name to Terminal Prompt (MacOS Catalina zsh)

Add Git Branch Name to Terminal Prompt (MacOS Catalina zsh)

screenshot

Open ~/.zshrc in your favorite editor and add the following content to the bottom.

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Original gist for Bash by Jose Quintana: https://gist.github.com/joseluisq/1e96c54fa4e1e5647940

@abrl91
Copy link

abrl91 commented May 22, 2021

Thanks! :)

@aphisiit
Copy link

aphisiit commented May 26, 2021

Thanks you

@Alekzv9
Copy link

Alekzv9 commented Jun 1, 2021

Thanks!

@doubleknd26
Copy link

doubleknd26 commented Jun 4, 2021

Thanks!

@ing-arriola
Copy link

ing-arriola commented Jun 6, 2021

Pretty useful!!
Thanks so much for sharing :)

@doubleknd26
Copy link

doubleknd26 commented Jun 22, 2021

I've been used it few days and It seems overwrite the first line when typing long command. I think the reason is the COLOR_XX.
Here is a fixed version and I added this page as a reference link.
https://gist.github.com/doubleknd26/c4fcd2e65c3397947248878065b644ed

Thanks Again!

@SoftwareTestr
Copy link

SoftwareTestr commented Jun 28, 2021

Thanks

@vadymcubic
Copy link

vadymcubic commented Sep 16, 2021

Thanks!

@hdchinh-freec
Copy link

hdchinh-freec commented Sep 25, 2021

thank you 💯 that's awsome

@jous32
Copy link

jous32 commented Oct 16, 2021

Awesome

@cjba850
Copy link

cjba850 commented Oct 21, 2021

I found this mucked up my wraparound - but the fix I found was to tell zsh the color definitions were Zero-length strings.

COLOR_DEF=$'%{\e[0m%}'
COLOR_USR=$'%{\e[38;5;243m%}'
COLOR_DIR=$'%{\e[38;5;197m%}'
COLOR_GIT=$'%{\e[38;5;39m%}'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Made a massive difference for me. Hope that helps someone else!

@Rah1x
Copy link

Rah1x commented Nov 17, 2021

Here is for ppl who have light blue background (Ocean theme) 😆

#/ Add git branch to prompt

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/\(\1\)/p'
}

NEW_LINE=$'\n'
COLOR_MAIN=$'\e[1;33m'
COLOR_END=$'\e[0m'

setopt PROMPT_SUBST
export PROMPT='${NEW_LINE}${COLOR_MAIN}%n %~${COLOR_END} $(parse_git_branch)${COLOR_MAIN}$:${COLOR_END}${NEW_LINE}'

Also notice ive added newlines before and after the prompt line to make the system really clean.

@DarylAranha
Copy link

DarylAranha commented Nov 21, 2021

Thank you 👍 😄

@ZhiMing96
Copy link

ZhiMing96 commented Nov 30, 2021

Thanks

@rd67
Copy link

rd67 commented Dec 16, 2021

Thank you.

@kerwin0104
Copy link

kerwin0104 commented Dec 24, 2021

Thank you!

@JulioCLop
Copy link

JulioCLop commented Dec 28, 2021

Very Helpful! Thanks!

@ggustafsson
Copy link

ggustafsson commented Jan 11, 2022

$ git branch --show-current
master

@eliozashvili
Copy link

eliozashvili commented Jan 18, 2022

So how can I save it so it will always work automatically? Because it resets everytime I close terminal

@itsalb3rt
Copy link

itsalb3rt commented Jan 24, 2022

thanks!!!

@benP2ER
Copy link

benP2ER commented Mar 31, 2022

Thanks for the script.
I ran into some issues on my system (macOS Monterey 12.0.1; zsh 5.8). When I autocomplete paths or use the arrow key to scroll through previous commands the formatting gets screwed up. Turns out the issues occur because of the ANSI color codes.

Solution:

The zsh shell has its own Prompt Expensions with visual effects. For colors you use %F and %f to escape the foreground color. (This also allows the usage of color aliases like %F{blue}This text is blue!%f)

Adjusted script:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'%f'
COLOR_USR=$'%F{243}'
COLOR_DIR=$'%F{197}'
COLOR_GIT=$'%F{39}'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Other Things:

To avoid non ascii characters causing this issue try wrapping them in a 'glitch' sequence which assumes everything inside is only one character long, like this: %{👌%G%}.

To reload and apply adjustments to the formatting use $ source ~/.zshrc in the zsh shell.

If you have a long working directory, you can shorten the path by only displaying the last n elements of the path. To display the last 2 elements replace %~ with %2~.

I also didn't want 2 spaces displayed, when the current working directory is not a repository so I adjusted the spacings:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1] /p'
}

COLOR_DEF=$'%f'
COLOR_USR=$'%F{243}'
COLOR_DIR=$'%F{197}'
COLOR_GIT=$'%F{39}'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%2~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}$ '

You could also shorten the script by removing the COLOR_... variables and using the extensions directly:

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

Hope this maybe helps anyone with the same issues.

@thiagovars
Copy link

thiagovars commented Apr 25, 2022

Works fine to me. Thanks a lot!

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