OS X Mountain Lion terminal tab name + open a new tab in the same directory

bashosx-mountain-lionterminal

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

Best Answer

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"