- Catalina
- Big Sur
- Monterey
- Ventura
- Sonoma
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} $ '
To reload and apply adjustments to the formatting use source ~/.zshrc
in the zsh shell.
- Thank you benP2ER for the useful additions in his comment
- Original gist by Jose Quintana: Add Git Branch Name to Terminal Prompt (Linux/Mac)
This is great thanks!
Is there a way to make it change as i move directories/branches?
Scenario 1
I am on branch
main
the prompt shows my branch asmain
i switch to branchfeature-X
the prompt still shows I am on branchmain
Scenario 2
Lets say I have 2 repositories
repository A
andrepository B
If I am on branchmain
inrepository A
and then runcd ../repository B
and I am onfeature-branch-X
in that repository, then the prompt still shows I am on main which is not true. Is there a way to change this?IDK if this is an issue that just I'm having or if its something I'm doing wrong etc.