Set Zsh Tab Title Automatically Based on Folder

in code


Pre-determined tab titles

My current task at work sees me rewriting an old application from scratch. A good part of this involves me going back and forth between the old and new project folders-call them “wigwam” and “spaklepony”-to compare code. This can be difficult to keep straight in tabs as a terminal user.

A quick walk of Stack Overflow and the ZSH chpwd hook led me to a two-step solution:

  1. Add hook function to set tab title.
  2. Add chpwd hook to evaluate folder name on shell initialisation and subsequent cd calls.

Set Window Title

# set-window-title 'Neenaw weewoo'
function set-window-title {
  echo -ne "\e]0;${1}\a"
}

Set Title on cd

function -set-project-folder-title {
    if [[ $PWD =~ "Projects/sparklepony" ]]; then
        set-window-title 'Sparklepony'
    elif [[ $PWD =~ "Projects/wigwam" ]]; then
        set-window-title 'Wigwam'
    else
        set-window-title $(basename $PWD)
    fi
}

chpwd_functions=(${chpwd_functions[@]} "-set-project-folder-title")
-set-project-folder-title


Replace Odd Numbers of Spaces Only

in code

factory_bot Email Factory

in code


Your email address will not be published. Required fields are marked *