Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

1) In Mountain Lion, through Preferences, you can change the setting of Terminal so that a new tab can be opened in the same directory as the opening tab. (i.e. if I am at ~/workspace and I open a new tab, then the new tab will be at ~/workspace).

2) Also in Mountain Lion, it is possible to have the title of the tab automatically reflect the current directory's basename via the following directive in ~/.profile:

# automatically change the title of the terminal window to the directory basename
PROMPT_COMMAND='echo -n -e "\033]0;${PWD##*/}\007"'

However, this also seems to remove terminal's ability to open a new tab in the same directory as the opening tab. That is, having 2) seems to make 1) ineffective. Is there a way to fix this issue so both of these things can happen at the same time?

enter image description here

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

The default PROMPT_COMMAND behavior is defined in /etc/bashrc and adds the path to the working directory to the window title in a way that preserves the Same Working Directory functionality (as file:// URL), and even allows browsing to ancestor directories by Command-clicking the title bar.

If you additionally want to set the tab title to the base name of the working directory, you need to preserve the original PROMPT_COMMAND. In your user profile's shell initialization file of choice, use the following:

function set_tab_title {
  echo -n -e "\033]0;${PWD##*/}\007"
}

PROMPT_COMMAND="set_tab_title ; $PROMPT_COMMAND"
share|improve this answer
    
it worked. Thanks so much. By the way, could you be so kind as to educate me a little bit about the difference between bashrc and profile, and /etc/bashrc vs ~/.bashrc. When are each loaded when an Apple Terminal starts up? Thank you again very much! –  platypus Jul 23 '13 at 20:13
    
@platypus How those files are used by bash. Additionally, note that on OS X's Terminal.app, every shell is an interactive login shell, whereas on Linux it's usually just interactive (non-login) shells. ~/.profile might be used by other shell programs. –  Daniel Beck Jul 23 '13 at 20:19
    
Thanks! It seems like ~/.profile is the last file to be loaded so it's safe to put all the custom configurations like PROMPT_COMMAND in it to be loaded by the terminal. Earlier you were saying PROMPT_COMMAND should be in ~/.bashrc FWIW, could you explain that a little? –  platypus Jul 23 '13 at 20:25
1  
@platypus Read that again. It only gets loaded if none of the other exist. PROMPT_COMMAND is something you want for all interactive shells. So it should probably be loaded if you run bash --login (when opening a new tab) as well as just bash. The file for that is ~/.bashrc. If you create ~/.bash_profile as well, that needs to source ~/.bashrc, otherwise it doesn't get loaded. –  Daniel Beck Jul 23 '13 at 20:33
add comment

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.