Skip to content

Instantly share code, notes, and snippets.

@robvanderleek
Last active January 18, 2024 13:09
Show Gist options
  • Save robvanderleek/9ff3a51833883ce8a51c4660c2790f9d to your computer and use it in GitHub Desktop.
Save robvanderleek/9ff3a51833883ce8a51c4660c2790f9d to your computer and use it in GitHub Desktop.
Show current Git branch in iTerm2

Show current Git branch in iTerm2

Terminal

If you want your iTerm2 terminal window display the current Git branch (when the current directory is inside a Git repository of course) add the following to your .zshrc:

set_terminal_title() {
    git_branch=`git branch 2> /dev/null |\
         sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'`
    echo -ne "\e]1;$git_branch\a"
}
export PROMPT_COMMAND="$PROMPT_COMMAND; set_terminal_title"
precmd() { set_terminal_title }

Also, make sure your iTerm2 configuration (Prefences > Profiles > General) has the following settings:

Settings-1

Settings-2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment