Customize terminal prompt for zsh to show git branch
Here is the code to put into .zshrc file to show the current folder name and the current git branch in the terminal prompt:
# Make terminal prompt show the current git branch in parentheses
git_prompt() {
local branch="$(git symbolic-ref HEAD 2> /dev/null | cut -d'/' -f3-)"
local branch_truncated="${branch:0:30}"
if (( ${#branch} > ${#branch_truncated} )); then
branch="${branch_truncated}..."
fi
[ -n "${branch}" ] && echo " (${branch})"
}
# Set the prompt
setopt PROMPT_SUBST
PROMPT='%{$fg[blue]%}%1~%{$fg[yellow]%}$(git_prompt)%{$reset_color%} $ '
Last updated