Skip to content

Instantly share code, notes, and snippets.

@reinvanoyen
Last active May 11, 2026 16:06
Show Gist options
  • Select an option

  • Save reinvanoyen/05bcfe95ca9cb5041a4eafd29309ff29 to your computer and use it in GitHub Desktop.

Select an option

Save reinvanoyen/05bcfe95ca9cb5041a4eafd29309ff29 to your computer and use it in GitHub Desktop.
Add Git Branch Name to Terminal Prompt (MacOS zsh)

Add Git Branch Name to Terminal Prompt (zsh)

Updated for MacOS with zsh

  • Catalina
  • Big Sur
  • Monterey
  • Ventura
  • Sonoma

screenshot

Install

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

Reload and apply adjustments

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

Credits

@benP2ER

benP2ER commented Mar 31, 2022

Copy link
Copy Markdown

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
Copy Markdown

Works fine to me. Thanks a lot!

@michaeljohncalvey

Copy link
Copy Markdown

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

This was super helpful, ty

@Wamae

Wamae commented Jun 14, 2022

Copy link
Copy Markdown

Lovely!, thank you.

@matthewbordas

Copy link
Copy Markdown

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.

Thank you!!

@rcrick

rcrick commented Aug 22, 2022

Copy link
Copy Markdown

Thanks!

@kevin-almeida-ilia

Copy link
Copy Markdown

thanks

@mannyanebi

Copy link
Copy Markdown

Thank you!

@saintmalik

Copy link
Copy Markdown

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

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

@carlosjarrieta

Copy link
Copy Markdown

Thanks you

@RodrigoIbarraSanchez

Copy link
Copy Markdown

Thanks!

@jsohndata

Copy link
Copy Markdown

Thanks!!!

@reinvanoyen

Copy link
Copy Markdown
Author

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.

Thank you for your deep-dive comment. I updated the gist with your solution and linked your comment.

@DigitalSolomon

Copy link
Copy Markdown

Great stuff.

@trontrytel

Copy link
Copy Markdown

Thank you!

@silviomedice

Copy link
Copy Markdown

Thanks :)

@amalsalimz

Copy link
Copy Markdown

finally!! thanks

@chunshao90

Copy link
Copy Markdown

Thank you!!!

@jake-betplusev

Copy link
Copy Markdown

this is awesome :)

@Dayglor

Dayglor commented May 23, 2023

Copy link
Copy Markdown

Nice! Thank you!

@saintmalik

Copy link
Copy Markdown

thanks

@giedriusksd

Copy link
Copy Markdown

Thanks

@cmiller96

Copy link
Copy Markdown

Thank you!

@nicholaide

Copy link
Copy Markdown

Thank you

@daCFniel

Copy link
Copy Markdown

It's perfect, thank you :)

@symplytheo

Copy link
Copy Markdown

Gracias

@Ramizdo

Ramizdo commented Oct 6, 2023

Copy link
Copy Markdown

Funciona, gracias..

@vnguyen13

Copy link
Copy Markdown

Works wonderfully. Thank you!!

@devih-21

Copy link
Copy Markdown

helpful, thanks bro

@vasu2912

Copy link
Copy Markdown

Looks Clean !!!

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