Skip to content

Instantly share code, notes, and snippets.

@halilim
Created January 4, 2024 11:43
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 halilim/d14c4d47f50ae01c35dc21c03a6b3771 to your computer and use it in GitHub Desktop.
Save halilim/d14c4d47f50ae01c35dc21c03a6b3771 to your computer and use it in GitHub Desktop.
Set iTerm2 subtitle to current git folder or working directory

Put this in shell init scripts (after iterm2_shell_integration):

function iterm2_print_user_vars() {
  iterm2_set_user_var pathShort "$(iterm2_custom_path_short)"
}

function iterm2_custom_path_short() {
  local dir
  dir=$(git rev-parse --show-toplevel 2> /dev/null || pwd)
  if [[ $dir == "$HOME" ]]; then
    dir='~'
  fi
  echo "${dir:t}"
}

And in iTerm2 settings > Profiles > General > Subtitle: \(user.pathShort)

Based on https://iterm2.com/documentation-scripting-fundamentals.html#setting-user-defined-variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment