Skip to content

Instantly share code, notes, and snippets.

@shanev
Created November 13, 2025 16:47
Show Gist options
  • Select an option

  • Save shanev/8e485a61a7e7a2a802608c949b7f9b98 to your computer and use it in GitHub Desktop.

Select an option

Save shanev/8e485a61a7e7a2a802608c949b7f9b98 to your computer and use it in GitHub Desktop.
Setup git worktree names in Ghostty tabs in zsh
set_tab_title_to_worktree() {
local title git_root
if command -v git >/dev/null 2>&1; then
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -n $git_root ]] && title=${git_root:t}
fi
[[ -z $title ]] && title=${PWD:t}
printf '\033]0;%s\007' "$title" # OSC 0 updates tab+window in most terms
printf '\033]1;%s\007' "$title"
printf '\033]2;%s\007' "$title"
}
typeset -ga precmd_functions
precmd_functions+=(set_tab_title_to_worktree)
# Ghostty's shell integration runs its own precmd hook that resets the title.
# This helper waits for that hook to exist, appends our retitle call to it,
# then removes itself to avoid running on every prompt.
_ghostty_title_patch() {
if [[ -z "${GHOSTTY_SHELL_FEATURES:-}" ]]; then
precmd_functions=(${precmd_functions:#_ghostty_title_patch})
return
fi
if typeset -f _ghostty_precmd >/dev/null; then
case ${functions[_ghostty_precmd]} in
*set_tab_title_to_worktree*) ;;
*) functions[_ghostty_precmd]+=$'\nset_tab_title_to_worktree' ;;
esac
precmd_functions=(${precmd_functions:#set_tab_title_to_worktree})
precmd_functions=(${precmd_functions:#_ghostty_title_patch})
set_tab_title_to_worktree
fi
}
precmd_functions+=(_ghostty_title_patch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment