This content originally appeared on DEV Community and was authored by Ahmet
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.
This content originally appeared on DEV Community and was authored by Ahmet
Ahmet | Sciencx (2021-11-04T07:10:32+00:00) Display Current Git Branch on iTerm2. Retrieved from https://www.scien.cx/2021/11/04/display-current-git-branch-on-iterm2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.