Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dayglor/091f9123ef4ebdbfb5597b3857a99960 to your computer and use it in GitHub Desktop.
Save Dayglor/091f9123ef4ebdbfb5597b3857a99960 to your computer and use it in GitHub Desktop.
Add Git Branch Name to Terminal Prompt (MacOS Catalina zsh)

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

Updated for MacOS Big Sur | Monterey | Ventura

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

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