Last Updated: February 25, 2016
·
6.303K
· martinsvalin

Show directory and git branch in iTerm tabs

Do you want to quickly be able to tell which project an iTerm tab is associated with, like this:

Picture

Set your PROMPT_COMMAND appropriately:

export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}$(__git_ps1 :%s)\007"'

This is for bash. Zsh does not have PROMPT_COMMAND; instead you would define the precmd function. Escaping is slightly different in zsh, but other than that it should be straightforward.

The project name corresponds to the repo's directory name. I use bash's parameter extraction to get the directory name without the full path. PWD##*/ takes the full path of the working directory and deletes everything matching up to the last slash.

The __git_ps1 function is provided by git-prompt.sh.

4 Responses
Add your response

If you want to show the currently running command when you're not at the prompt, you can set the iterm title in a DEBUG trap.

All in all, my solution is:

function iterm_title { echo -ne "\033]0;$@\007"; }
trap 'iterm_title $BASH_COMMAND' DEBUG
export PROMPT_COMMAND='iterm_title ${PWD##*/}$(__git_ps1 :%s)'
over 1 year ago ·

I implemented the bash solution in zsh here: https://gist.github.com/teoljungberg/5772474

over 1 year ago ·

nice! thanks!

over 1 year ago ·

That was easy :presses red button:

over 1 year ago ·