Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I use the Mac Terminal with a hand full of tabs each assigned to a different working directory. I have configured it to open new windows with the same working directory. Nevertheless, when I am quitting Terminal, and restart it it rebuilds all the tabs, their names, even shows me the last output in the window but stays in the user home directory and does not restore the latest working directory of each tab.

What am I doing wrong? Can this be caused by some setting in the ~/.bash_profile?

share|improve this question
    
I think you can find what you are looking for in this thread. apple.stackexchange.com/questions/863/… –  dennis.hempler Apr 27 '12 at 12:50
    
So am I wrong in assuming this should be supported out of the box? –  Oliver Gierke Apr 29 '12 at 11:09

3 Answers 3

up vote 7 down vote accepted

(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
share|improve this answer
1  
Thanks for the reply but I don't seem to be able to get that working. I have removed my .bashrc (although I couldn't spot any prompt command customization. Now I do the following: 1. Open a new Terminal window (opens in my user's home directory) 2. cd Documents 3. Quit Terminal 4. Open Terminal 5. Window reopens displaying the directory change output but still located in the user's home directory. –  Oliver Gierke Aug 1 '12 at 12:32
    
@OliverGierke Terminal creates login shells, so also check for ~/.bash_profile, ~/.bash_login, and ~/.profile. –  Chris Page Aug 3 '12 at 16:23
    
I don't have the latter two, the former now executes ~/.bashrc as per your suggestion in your answer. –  Oliver Gierke Aug 5 '12 at 17:58
2  
I got it figured out, Chris. My /etc/bashrc seems to have been tweaked by an install script or whatever. There was a /etc/bashrc.orig that contained some additional config (which I assume to be the original setup) gist.github.com/3273170. Re-adding that to the /etc/bashrc made things working as expected again. Thanks a lot for help and patience! –  Oliver Gierke Aug 6 '12 at 10:16
    
I don't change the $PROMPT_COMMAND in my .bashrc and I only have the system default /etc/bashrc. Any further suggestions? –  Richard Jun 28 '13 at 20:06

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

share|improve this answer

I wrote up a blog post on how to do this for csh/tcsh before I discovered this answer; if anyone else comes here looking for that, here it is:

if ("$TERM_PROGRAM" == "Apple_Terminal") then
  alias precmd 'printf "\033]7;%s\a" "file://$host$cwd:ags/ /%20/"'
endif

Add that to your .cshrc or .tcshrc as appropriate.

(Like Apple's builtin bash support, this solution uses no external programs other than printf, at the cost of only escaping spaces. If you need to escape other special characters, you'll have to work a little harder to find a more comprehensive solution.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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