To those fellows who are quite familiar with the Linux Shell environment, they must be knowing the 'open new tab under the same working directory' function. However, the pre-installed Terminal app on OS X seems like not supporting this function well naturally. Thus for countless times I have to find all the way back to the last opened directory whenever I decided to initiate a new window or a tab. This even once made me believe that my most favourable 'reopen all windows after system reset' function on Mac OS X is not supported by the Terminal app, until I spent a whole day to figure out the reason and solution. Now I wish to share my humble experience about this with you guys.
I. Theory
Some of you who have done some research about the Mac OS X Terminal might have found out that, in 'Terminal > Preferences > General' panel, actually there is an option named 'New windows/tabs open with: Same profile/ Same Working Directory'. However, some of you guys might also find out that the Terminal does not behave as expected afterwards. Why?
This is somehow related to the way Terminal works. In fact, this selection only activates the function that the tool opens a new Window/Tab using the working directory recorded in its 'internal working directory buffer'. Similar function dependent to the 'internal working directory buffer' includes 'Terminal > Preferences > Profile > Window/Tab > Title: Working directory or document', you will find the title of the window/tab does not change with the working directory as well as if the 'new tab open in working directory' function does not work. The reason of these is, the Terminal itself has no way to know about the 'working directory' unless it is told by the user through command line. To those who might have these functions working perfectly well, they should have used bash instead of other shells and didn't change the default configuration in the 'etc/bashrc' or '~/bash_profile', for as the default shell on OS X, bash is preset to enable this 'send auto-command to inform of working directory' function. If you are using other shell, like zsh or csh, then you will have to enable this function by yourself.
II. Method
It is quite easy to get things working here. The correct way to inform the Terminal of the correct working directory is by sending the OSC (Operating System Command) in the command line. OS X actually has a hint about this in 'Terminal > Preferences > General' saying in blue font 'Escape sequence...". As for zsh or csh, there is a function that is called every time before you execute any other commands, named 'precmd()'. The trick that we will be doing here is just to reconstruct this function to the OSC that updates the working directory buffer of the terminal. Specifically, we need to add the following script to the 'etc/csh.cshrc' for c-shell or the '/etc/zshrc' for z-shell.
[source lang="bash"]
alias precmd 'printf "\033]7;file://${HOST}:$cwd\007"'
[/source]
The meaning of the script, is to replace the precmd function with an alias of the printf command that send the OSC to the terminal. The format of the OSC command is '\033]command ID; command data\007', where '\003' and '\007' are called 'command head' and 'command end' and could also be replaced with '\e' and \a' on z-shell. The command ID of '7' is 'Working directory', and the command data includes the correct working directory in a format like 'file://hostname:directory'. In this command, ${HOST} is the environment variable for host name, and $cwd is the variable for current working directory.
If this script doesn't work for you, you may want to try to type only the part inside of the ' marks in the command line to see if it works. If it does work, the problem may lay on the calling mechanism of the precmd function, maybe you have written the script in the wrong configure file so it is not executed. If it does not work, maybe you have written the printf command in a wrong way.