Why does Terminal not honor preferences?
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:
- Toggling settings for New Tab or New Window
- Renaming or adding
return
at the first line of.zshrc
,.zsh*
files - All of the suggestions in Open new Terminal tab in current working directory doesn't 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 (includingnix-darwin
)
Metadata:
OS macOS Catalina 10.15.7 (19H2)
Terminal 2.10 (433)
zsh 5.7.1 (x86_64-apple-darwin19.0)
If you click on "Escape sequence…" in that screenshot, you get to see the following:
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
orzsh -f
. - You have a file
${ZDOTDIR:-$HOME}/.zshenv
or${ZDOTDIR:-$HOME}/.zprofile
in which you havesetopt NO_rcs
orsetopt NO_globalrcs
- The parameter
$TERM_PROGRAM
does not have the valueApple_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 byadd-zsh-hook
.
What you can do to debug:
- Start a subshell without non-global config files and see if the bug happens there, too:
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 doingcd $(mktemp -d); HOME=$PWD ZDOTDIR=$PWD zsh
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 theprecmd
hook it sets up.