Why does Terminal not honor preferences

command lineterminalzsh

Sometime in the last week my Terminal stopped honoring the "New tabs open with: Same Working Directory" setting. Testing with "New windows …" gave the same result.

My shell is zsh.

I was able to get this to work by following an answer here from Dan Rosenstark
https://apple.stackexchange.com/a/340778/398648

# http://superuser.com/a/315029/4952
# Set Apple Terminal.app to resume directory... still necessary 2018-10-26
if [[ $TERM_PROGRAM == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]] {
  function chpwd {
    local SEARCH=' '
    local REPLACE='%20'
    local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
    printf '\e]7;%s\a' "$PWD_URL"
  }
  chpwd
}

But: I do not know how I could have determined this for myself, or how I could have debugged a similar problem.

Any tips or tricks to figure out what those initial steps of Terminal are would be greatly appreciated.

Things that did not work:

Other potentially useful info:

  • The "Same Profile"/"Default Profile" settings are honored when changed
  • Adding pwd as the first line of .zshrc shows the directory is already /home/<myuser>
  • I have installed and uninstalled some tools and projects involving nix recently (including nix-darwin)

Terminal Preferences

Metadata:

OS        macOS Catalina 10.15.7 (19H2)
Terminal  2.10 (433)
zsh       5.7.1 (x86_64-apple-darwin19.0)

Best Answer

If you click on "Escape sequence…" in that screenshot, you get to see the following:

enter image description here

What this doesn't tell you, though, is that this is normally taken care of in the file /etc/zshrc_Apple_Terminal, which is called from /etc/zshrc. There are several things that can prevent /etc/zshrc_Apple_Terminal from being called or which can override what it tries to do:

  • You are starting Zsh with zsh -d or zsh -f.
  • You have a file ${ZDOTDIR:-$HOME}/.zshenv or ${ZDOTDIR:-$HOME}/.zprofile in which you have setopt NO_rcs or setopt NO_globalrcs
  • The parameter $TERM_PROGRAM does not have the value Apple_Terminal.
  • Zsh does not have read access to /etc/zshrc_Apple_Terminal.
  • When /etc/zshrc_Apple_Terminal gets sourced, the parameter $INSIDE_EMACS is non-zero.
  • You define a function precmd sometime after /etc/zshrc_Apple_Terminal gets sourced, which then overrides the one defined by add-zsh-hook.

What you can do to debug:

  • Start a subshell without non-global config files and see if the bug happens there, too:
    cd $(mktemp -d); HOME=$PWD ZDOTDIR=$PWD zsh
    
    If the problem does not occur in this subshell, then that means the problem is somewhere in your non-global dotfiles. You can get a list of these files by doing
    ls ${ZDOTDIR:-$HOME}/.z(log(in|out)|profile|sh(env|rc))(-^/)
    
  • Restart Zsh with exec zsh -vx and study the output from the beginning to see if /etc/zshrc_Apple_Terminal gets sourced and whether anything later overrides the precmd hook it sets up.