Skip to content

Instantly share code, notes, and snippets.

@abdes-zakari
Created December 3, 2021 15:23
Show Gist options
  • Save abdes-zakari/3eb232acd89ee697bf5d76d9862543d7 to your computer and use it in GitHub Desktop.
Save abdes-zakari/3eb232acd89ee697bf5d76d9862543d7 to your computer and use it in GitHub Desktop.
Add Git Branch Name to Terminal Prompt on Mac (Big Sur)

Add Git Branch Name to Terminal Prompt on Mac (Big Sur)

Open ~/.zprofile or ~/.zshrc in your favorite editor and add the following content to the bottom. ( if the file ~/.zprofile or ~/.zprofile does not already exist create it)

# Git branch in prompt.
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} $ '

then source ~/.zshrc or source ~/.zprofile to refresh changes

Differences between .zprofile and zshrc:

~/.zprofile is only sourced when zsh is run as login shell, e.g. when logging in on the console or via SSH.

It will not be sourced by zsh when opening a new terminal or starting a new zsh session from within a running session. Anything you need in all interactive sessions, should be set in ~/.zshrc. Anything you need in all zsh sessions, including scripts, should be set in ~/.zshenv.

source: https://serverfault.com/a/901476/943175

@mansoorahmedkhan
Copy link

I edited my /etc/zshrc file and quit the terminal and start it again. Worked as expected. Thanks.

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