using iTerm badges with git projects

All of my projects run on almost the same stack and my terminal usually has more than a handful of them running at the same time.

When switching tabs in iTerm I always have to decipher what project is in which tab. Usually, the way to deduce this is by the path in the prompt, but that is not always visible.

iTerm has this awesome feature that lets you place whatever in a layer above your regular terminal content. It’s called ‘badges’ and with it you can add a textual indicator to whatever project you are running.

But then, you have to remember to add one. And to update it in case you change directories.

Except, iTerm allows you to specify one dinamically via a special function/variable that is available if you install its shell integration (you should!).

With that, you can define a special function in your bash profile that tells iTerm what text to put in a badge.

The following function leverages git to find the home directory of the current git repository you are on and then extracts just the directory name. That name is used as the badge.

source ~/.iterm2_shell_integration.`basename $SHELL`
function iterm2_print_user_vars() {
    iterm2_set_user_var gitProyectDir $(basename $(git rev-parse --show-toplevel 2> /dev/null) 2> /dev/null)

Whenever you enter a directory that is a git project, your badge will reflect its name.