Apple - How to make Mac Terminal restore working directories when restarting

(For reference, we’re talking about the Resume feature of Mac OS X Lion 10.7 and later.)

Terminal automatically restores the working directory if you’re using the default shell, bash. If you’re using some other shell, you’ll need to adapt the code in /etc/bashrc to send an escape sequence to communicate the working directory to Terminal so it can restore the directory later for Resume. If you’re using zsh, see my answer to Resume Zsh-Terminal (OS X Lion), in which I include the appropriate code for zsh.

If you have a custom ~/.bash_profile or ~/.bashrc you may need to ensure that you’re not undoing the default behavior by modifying /etc/bashrc’s customizations. In particular, it sets the PROMPT_COMMAND environment variable to send the escape sequence at each prompt. If you customize that variable, you’ll need to prefix or append your code to the current value, e.g.:

PROMPT_COMMAND="<your code here>;$PROMPT_COMMAND"

Also, generally, ~/.bash_profile should execute ~/.bashrc:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

If you are using Bash-It, you may run into the problem of $PROMPT_COMMAND being overwritten by it.

As mentioned it is used by OS X to restore cwd in new tabs. Bash it should append values, not override them.

But a workaround for now would be to add the following line to your ~/.bash_profile

source $BASH_IT/bash_it.sh
export PROMPT_COMMAND="$PROMPT_COMMAND;update_terminal_cwd;"

For more info checkout the issue tracker for updates: https://github.com/revans/bash-it/issues/240

And the Apple reference for it https://developer.apple.com/library/mac/documentation/darwin/reference/manpages/man1/sh.1.html

Tags:

Terminal