OS X El Capitan 10.11 Terminal 2.6 won't open new tabs with same working directory

2

I am using the default settings for Terminal, but when I create a new tab my working directory is always set to my home folder instead of the current working directory of the previous tab.

Here's an image of my Terminal Preferences:

http://i.stack.imgur.com/TIIzN.png

As you can see in the General settings, "New tabs open with:” is set to “Same Working Directory". That does not seem to be working. I think I remember this working in older versions of Terminal.

My default login shell is set to /bin/zsh, and I have no ~/.zshrc.

I would like to be able to solve this issue without installing something like oh-my-zsh or having to add something to ~/.zshrc. This should work without any of that. (At least it does in iTerm2.)

Chase Stubblefield

Posted 2015-11-20T23:12:59.117

Reputation: 21

Update: This works if my default login shell is /bin/bash. I believe this has something to do with the file /etc/bashrc_Apple_Terminal, which sets the proper escape sequences for the shell to notify Terminal of the current working directory. There is no equivalent file for zsh and the /etc/zshrc file is very minimal. – Chase Stubblefield – 2015-11-20T23:31:50.363

Different sessions shouldn't talk to each other, unless it is handled on the terminal emulator level (which is not the case here, unless you convince Apple to do that), or you write some data to disk and load them later. I never bothered to look into /etc/bashrc_Apple_Terminal, but I know it writes session files into ~/.bash_sessions, from which it can load path from a former session (I suppose). You can do something similar in Zsh, but the requirement of not modifying ~/.zshrc (I assume by that you are excluding all runcoms) is simply unrealistic. – 4ae1e1 – 2015-11-21T02:28:59.737

@4ae1e1: This question is about communicating the current working directory to Terminal (via escape sequences) and is unrelated to the shell command history. – Chris Page – 2015-11-28T13:11:30.477

Answers

2

By default on OS X, Bash is configured to communicate the current working directory to Terminal using escape sequences at each prompt, using the PROMPT_COMMAND environment variable.

Prior to OS X El Capitan 10.11, this code is found in /etc/bashrc. In 10.11 and later, it has been moved to the Terminal-specific /etc/bashrc_Apple_Terminal file and /etc/bashrc executes the appropriate terminal-specific file.

Current versions of Oh My Zsh! have similar code to communicate the working directory to Terminal.

The most likely reason this stopped working is that you changed your configuration. Perhaps you were using Bash before or you had Oh My Zsh installed and now you don't. Another possibility is that you used to have your shell configured to set the terminal window or tab (aka “icon”) title to contain the working directory pathname: as a convenience for older configurations, if the working directory isn't explicitly set but the window or tab title contains a valid local pathname, Terminal will treat it as the current working directory.

Chris Page

Posted 2015-11-20T23:12:59.117

Reputation: 2 793

2

A simple way to ensure that Terminal.app's "New windows/tabs open with: Same Working Directory" option still works with zsh as your login shell is to instruct zsh to communicate changes in the current working directory to Terminal.app via escape sequence. I've done so in my .zshrc with this:

# tell terminal.app about cwd so new tabs open in same dir
tell_terminal_cwd() {
   cwd=$(print -rD $PWD)
   echo -ne "\033]0;${cwd}\007"
}
precmd_functions+=(tell_terminal_cwd)

Terminal.app's instructions on this topic say provide the current working directory as a properly encoded file:// URL including hostname, but I've found that the above works fine and also looks nice in the tab title.

Jeremiah Boyle

Posted 2015-11-20T23:12:59.117

Reputation: 21

0

Like you, I wanted to do this without loading all of Oh-My-Zsh.

Turns out that, as of 4e306887 or so (why doesn't OMZ appear to have any concept of ‘versions!?’ Bad software-development practice. ಠ_ಠ), the termsupport.zsh file they include handles this for you. You'll have to rip the functions.zsh file out, as well.


Personally, I use the super-lightweight (like, I read all of the code before starting to use it because it's a ≤500-line file) Zgen.zsh. (It explicitly supports using OMZ modules and such without loading the rest of OMZ!) If you choose to do so, here's the solution with that:

if ! zgen saved

   ...

   # Steal omz's termsupport for Apple Terminal. (Hacky; will have to
   # watch to see if this changes.) - <http://superuser.com/a/1031708/22030>
   zgen load robbyrussell/oh-my-zsh lib/functions.zsh
   zgen load robbyrussell/oh-my-zsh lib/termsupport.zsh

   ...

fi

ELLIOTTCABLE

Posted 2015-11-20T23:12:59.117

Reputation: 1 207