2

One of my Macs is running Mac OS Mojave and so for parity with my Catalina machine I switched the shell to zsh. Not sure exactly when but at some point I noticed the current working directory was never being displayed either in the prompt itself or in the title of the tab.

I found I way to restore the title to the prompt by adding this to my ~/.zprofile file: PROMPT='%~ '. However, I have not been able to restore the directory name to the tab title:

Terminal window with tabs

This is quite frustrating when dealing with multiple tabs, as I do daily. It would seem to me that everything in the Terminal's settings is correct:

Terminal Preferences

Am I missing something or have I encountered a strange bug?

5
  • Open the Tab-tab in Terminal Prefs > Profile > [Profile name] (instead of the Window-tab) and check the radio buttons!
    – klanomath
    Commented Dec 17, 2019 at 23:41
  • I haven't tested this, but try copying the /etc/zshrc and /etc/zshrc_Apple_Terminal files from your Catalina system to the Mojave system (after backing up the original Mojave zshrc). Commented Dec 18, 2019 at 7:39
  • @klanomath Tried this, the same boxes are checked!! Commented Dec 18, 2019 at 9:08
  • @GordonDavisson I won't be able to try this until Friday but will give it a go Commented Dec 18, 2019 at 9:08
  • I should note, changing back to bash fixes it entirely. Something's up with zsh Commented Dec 18, 2019 at 9:46

3 Answers 3

3

Solution: Copy the zsh setup files (/etc/zshrc and /etc/zshrc_Apple_Terminal) from a Catalina system to the older system (preferably after backing up the original equivalent(s)).

I haven't tested this in Mojave, but I did in High Sierra, and it works fine there.

Explanation of the problem: Setting the Terminal window/tab title requires running some code (e.g. a shell function) to send the appropriate escape sequences, encoded version of the directory name, etc to the Terminal. In older versions of macOS, this was handled in bash by having /etc/bashrc run /etc/bashrc_Apple_Terminal, which defined a suitable function and set PROMPT_COMMAND to tell bash to run it each time it prompted for a new command. But since zsh wasn't the default shell, Apple didn't bother to create the equivalent setup scripts for zsh.

In Catalina, zsh is the default shell, so they wrote analogous setup steps and added them to /etc/zshrc and /etc/zshrc_Apple_Terminal. The mechanics are different (for example, zsh uses add-zsh-hook precmd instead of the PROMPT_COMMAND variable), but it does essentially the same thing. These files are not dependent on anything in Catalina; you can simply copy them to older systems, and they should work the same as they do in Catalina.

1

Not as a full answer, but to add to this if all the above isn't working as you've amended the PROMPT_COMMAND in your own bashrc files.

So, if you have something like ...

PROMPT_COMMAND='__git_ps1 "$PS1_COMBINED" "$PS1_PROMPT";'

(My PS1_COMBINED has a lot of things in it.)

What you'll notice is the original PROMPT_COMMAND is now lost. This is the one that /etc/bashrc sets up to include (eventually as there is another file involved), the instruction

PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"

For /etc/zshrc, the "bit" is

    # Register the function so it is called at each prompt.
    autoload -Uz add-zsh-hook
    add-zsh-hook precmd update_terminal_cwd

So slightly different, but as I'm on Bash and not Zsh, hopefully a little more info on what I've found.

1

Just use the escape sequences directly. This way you can also name tabs with custom names like "build", "test", "debug" &c. Keep in mind there are two kind of "titles": one is for window title, one is for icon name (the tabs' names). There is even a stack for these.

The bash functions you can put in your profile and $PROMPT_COMMAND:

  • To change window title, wintitle { echo -en "\e]2;$1\x01\e\\"; }
  • To change icon name, iconname { echo -en "\e]1;$1\x01\e\\"; }
  • To change both, wintitleiconname { echo -en "\e]0;$1\x01\e\\"; }
  • To change working directory, setworkdir { echo -en "\e]7;$1\a"; } ($1 should be a proper encoded file:// URL)

src: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#:~:text=%3B%20Pt%20BEL-,OSC%20Ps%20%3B%20Pt%20ST,-Set%20Text%20Parameters

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .