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?
I recommend replacing
echo -n -e
withprintf
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.550it 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.
– Daniel Beck – 2013-07-23T20:19:10.010~/.profile
might be used by other shell programs.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 runbash --login
(when opening a new tab) as well as justbash
. The file for that is~/.bashrc
. If you create~/.bash_profile
as well, that needs tosource ~/.bashrc
, otherwise it doesn't get loaded. – Daniel Beck – 2013-07-23T20:33:35.930