Skip to content

Instantly share code, notes, and snippets.

@wellcaffeinated
Created September 26, 2023 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellcaffeinated/64b712db2b01cdb0d364f212d1eb9240 to your computer and use it in GitHub Desktop.
Save wellcaffeinated/64b712db2b01cdb0d364f212d1eb9240 to your computer and use it in GitHub Desktop.
Setting iTerm2 tab name to shortened current directory in zsh

ensure that Profile -> Terminal -> Terminal may report title is checked

Then add the following to .zshrc

# set iterm title
iterm_title() {
  echo -ne "\e]1;$1\a"
}

# set iterm tab title on prompt
iterm_title_pwd() {
  # shorten path
  p="${PWD/#$HOME/~}"
  # replace parent directories with first letter but not the last one
  p=$(echo $p | sed -E 's|([^/])[^/]*\/|\1/|g')
  iterm_title "$p"
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd iterm_title_pwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment