Skip to content

Instantly share code, notes, and snippets.

@reinvanoyen
Last active April 12, 2024 22:18
Show Gist options
  • Save reinvanoyen/05bcfe95ca9cb5041a4eafd29309ff29 to your computer and use it in GitHub Desktop.
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

@amalsalimz
Copy link

finally!! thanks

@chunshao90
Copy link

Thank you!!!

@jake-betplusev
Copy link

this is awesome :)

@Dayglor
Copy link

Dayglor commented May 23, 2023

Nice! Thank you!

@saintmalik
Copy link

thanks

@giedriusksd
Copy link

Thanks

@cmiller96
Copy link

Thank you!

@nicholaide
Copy link

Thank you

@daCFniel
Copy link

It's perfect, thank you :)

@symplytheo
Copy link

Gracias

@Ramizdo
Copy link

Ramizdo commented Oct 6, 2023

Funciona, gracias..

@vnguyen13
Copy link

Works wonderfully. Thank you!!

@devih-21
Copy link

helpful, thanks bro

@vasu2912
Copy link

Looks Clean !!!

@csarrvas
Copy link

Thanks!! 👍

@pychap
Copy link

pychap commented Dec 21, 2023

Thank you!

@z-coder-hub
Copy link

Thank you!

@emcfarlane
Copy link

Thank you!

@gideonokyere
Copy link

Thanks

@TrevorPawlewicz
Copy link

This is exactly what I was looking for... thank you!

@beitomartinez
Copy link

Simple and exactly what I was looking for... thanks!! 💯

@joyoy96
Copy link

joyoy96 commented Mar 26, 2024

but it adds unneccesary space when I opening the non git folder, could we do something about that?

the syntax to get the branch also could be simpler we could do this now

git branch --show-current

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