MacOS – Terminal Tab Title after SSH Session

macossshterminal

In Mac OS X (I'm on 10.6.8, though I believe other versions are the same) ssh'ing into a remote machine changes the title of the current tab in Terminal. Annoyingly, when I disconnect from the remote machine, either explicitly with exit or via a timeout, the title of the tab doesn't change back to what it was.

On an almost daily basis this nearly causes me to suffer a heart attack, when I'm typing away performing some command or other, only to see out of the corner of my eye that the tab is still named user@remotehost. I'm a cautious user that always tends to pwd etc to confirm my location before doing anything, yet it still catches me in a moment of panic when I'm doing DROP DATABASE x and I see the remote host name in the tab.

Long story short, is there a way to revert this title when disconnecting from a remote host, or do I have to stick with opening a new tab every time I disconnect, to reset the title?

Best Answer

Add a PROMPT_COMMAND to your .bash_profile

export PROMPT_COMMAND="echo -ne '\033]0;${USER}@${HOSTNAME}\007';$PROMPT_COMMAND"

The PROMPT_COMMAND gets executed every time the prompt is displayed.

Please note that we include the existing PROMPT_COMMAND environment variable, such that we do not lose any existing settings (i.e. update_terminal_cwd).