3

My environment

  • Terminal.app 2.9.4 on macOS
  • zsh 5.7.1

When I used bash, Terminal could open current directory in a new tab, but it have not been able to do that since I changed to zsh.

In the app setting, even though it is set as "Same Working Directory".

enter image description here

If I would write something on .zshrc or .zprofile, would it be able to do that?

1

Found a solution here: https://blog.callstack.io/supercharge-your-terminal-with-zsh-8b369d689770

# Open new tabs in same directory
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
  function chpwd {
    printf '\e]7;%s\a' "file://$HOSTNAME${PWD// /%20}"
  }
  chpwd
fi

Note that the only percent-encoding it does on the path is to replace spaces with "%20". This should be replaced with a more robust encoding.

  • Thanks to the article you found, I've realized what I wanted to do! And I’d examine your viewpoint. Thank you very much!! 🙇‍♂️ – tsubasakat May 6 at 15:53
1

Click the blue text:

Terminal preferences window with popup showing how to notify Terminal of the current working directory

So you just need to add that little escape sequence to your prompt so that Terminal knows where you are. Easy!

Or, if you think that's a little cryptic, let's take a look at how bash manages to pull it off. Checking man bash reveals that when bash is invoked as an interactive login shell (this should be the default when opening a Terminal window or tab), one of the files it references is /etc/profile.

A quick peek at /etc/profile shows that it may load /etc/bashrc.

And looking inside /etc/bashrc shows that it tries to load /etc/bashrc_$TERM_PROGRAM. Quick check:

> echo $TERM_PROGRAM
Apple_Terminal

A less /etc/bashrc_Apple_Terminal later, and jackpot! Right up near the top:

# Tell the terminal about the current working directory at each prompt.

Followed by a function that does just that, ripe for inclusion in a (bash) prompt. I'm going to refrain from copying the function here, as I'm not entirely sure of the legality of doing so, though I will include the last line of the function, as I believe that's trivial enough to be fair use:

printf '\e]7;%s\a' "file://$HOSTNAME$url_path"

This is basically that cryptic escape sequence from the preferences window popup. The rest of the function is entirely about setting up the $url_path variable with the necessary percent_encoding. You'll need to translate that stuff to zsh or just extract it to an external bash script and call that, though firing up a bash instance every time might slow your prompt down a bit.

  • Thanks to you, I’ve roughly learned how that was being realised! They seem to be very well configured. I’ll try to translate it in reference to them🙇 – tsubasakat Apr 25 at 23:41
0

Just do the following keyboard shortcut:

cmd + ctrl + o

  • I tried that way, but there is no response. I also tried Ctrl + Cmd + N and Opt + Cmd + N or O etc., but the results are the same. – tsubasakat Apr 23 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.