GNU Screen – How to Change New Window Name

gnu-screenterminalwindow title

I can change the name of a window with Ctrl-a Shift-a. Instead of editing several window names by hand, is there a way to have them automatically named after the current directory?

Best Answer

Make your shell change the window title every time it changes directory, or every time it displays a prompt.

For your ~/.bashrc:

if [[ "$TERM" == screen* ]]; then
  screen_set_window_title () {
    local HPWD="$PWD"
    case $HPWD in
      $HOME) HPWD="~";;
      $HOME/*) HPWD="~${HPWD#$HOME}";;
    esac
    printf '\ek%s\e\\' "$HPWD"
  }
  PROMPT_COMMAND="screen_set_window_title; $PROMPT_COMMAND"
fi

Or for your ~/.zshrc (for zsh users):

precmd () {
  local tmp='%~'
  local HPWD=${(%)tmp}
  if [[ $TERM == screen* ]]; then
    printf '\ek%s\e\\' $HPWD
  fi
}

For more information, look up under Dynamic titles in the Screen manual, or under “Titles (naming windows)” in the man page.

Related Question