Skip to content

Instantly share code, notes, and snippets.

@MiguelERuiz
Last active March 25, 2021 22:01
Show Gist options
  • Save MiguelERuiz/6d92eb86fce6b6bbeba0082bc28768c5 to your computer and use it in GitHub Desktop.
Save MiguelERuiz/6d92eb86fce6b6bbeba0082bc28768c5 to your computer and use it in GitHub Desktop.
git branch into the `prompt` variable in ZSH (Mac Terminal)
# inspired by:
# https://medium.com/pareture/simplest-zsh-prompt-configs-for-git-branch-name-3d01602a6f33
# https://medium.com/dev-genius/customize-the-macos-terminal-zsh-4cb387e4f447
# http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Prompt-Expansion
# Find and set branch name var if in git repository.
function git_branch_name()
{
branch=$(git symbolic-ref HEAD 2> /dev/null | awk 'BEGIN{FS="/"} {print $NF}')
if [[ $branch == "" ]];
then
:
else
echo ' - ('$branch')'
fi
}
# Enable substitution in the prompt.
setopt prompt_subst
# Config for prompt. PS1 synonym.
# miguel@~/repo - (master) >
prompt='%F{red}%n%f@%F{green}%~%f%F{magenta}$(git_branch_name)%f > '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment