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

7

3

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

platypus

Posted 2013-07-23T19:22:50.873

Reputation: 205

Answers

10

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"

Daniel Beck

Posted 2013-07-23T19:22:50.873

Reputation: 98 421

I recommend replacing echo -n -e with printf in this instance, to avoid interpreting the contents of the directory name by separating the escape sequence from its content (this also means you can use the mnemonic \e and \a instead of octal for the control characters): printf '\e]0;%s\a' "${PWD##*/}" – Chris Page – 2015-02-17T02:01:35.550

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 – 2013-07-23T20:13:22.560

@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 – 2013-07-23T20:19:10.010

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 – 2013-07-23T20:25:29.307

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 – 2013-07-23T20:33:35.930