Display Current Git Branch on iTerm2

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 wi…


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 🙂

Image description

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}%% '

Image description

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.

Image description

Image Source


This content originally appeared on DEV Community and was authored by Ahmet


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Display Current Git Branch on iTerm2." Ahmet | Sciencx - Thursday November 4, 2021, https://www.scien.cx/2021/11/04/display-current-git-branch-on-iterm2/
HARVARD
Ahmet | Sciencx Thursday November 4, 2021 » Display Current Git Branch on iTerm2., viewed ,<https://www.scien.cx/2021/11/04/display-current-git-branch-on-iterm2/>
VANCOUVER
Ahmet | Sciencx - » Display Current Git Branch on iTerm2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/04/display-current-git-branch-on-iterm2/
CHICAGO
" » Display Current Git Branch on iTerm2." Ahmet | Sciencx - Accessed . https://www.scien.cx/2021/11/04/display-current-git-branch-on-iterm2/
IEEE
" » Display Current Git Branch on iTerm2." Ahmet | Sciencx [Online]. Available: https://www.scien.cx/2021/11/04/display-current-git-branch-on-iterm2/. [Accessed: ]
rf:citation
» Display Current Git Branch on iTerm2 | Ahmet | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.