Date Category tech

First, the code (add to your ~/.bash_profile):

function git-title {
    local title
    if ! title="branch: `git rev-parse --abbrev-ref HEAD 2>/dev/null`"; then
        # Not a git repository
    echo -ne "\033]2;$title\007"

export PROMPT_COMMAND="git-title"

How it works: the git-title function uses the terminal escape sequence to set your title to “branch: foo” if your current working directory is inside a git repository.

The PROMPT_COMMAND variable allows you to run a command every time bash is about to render a new prompt. I think the intent is that you can echo additional information to the terminal to make your prompt more robust, but I’m taking advantage of this hook to just make sure the title updates whenever you have potentially entered a repository, changed branch, etc.