As a Windows user, I generally use Conemu Console for my console and it has a sweet future in which you can see the current git branch on the terminal. After using Mac I wanted to see the same future on iTerm2. I found the way and I want to share it with you 🙂
.zshrc file
Firstly create and edit a .zshrc file that will use for terminal configuration.
touch ~/.zshrc; open ~/.zshrc
parse_git_branch
Customize your terminal whatever you want with this file. You can use the following bash code to display the git branch.
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
And that is it! Restart your terminal. You will see the current branch on your terminal 🙂
More customization
If you want more customization you can edit the PROMPT variable in the .zshrc file. For example, you can add your current computer user.
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}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
More color!
If you want to change color, just edit the color code.
Change COLOR_USR='%F{243}' to COLOR_USR='%F{229}'
You can find the color code schema in the following image.
Top comments (4)
Worked like the charm! Thanx for the colour overview!
Perfect!!
That kind of things are super handy!
Thank you!!
Awesome man, thanks!!
Thanks so much for your helpful post, Ahmet! That was exactly what I needed.