Sunday, August 13, 2017

Setting Mac ITerm tab titles to the current directory

It is easy to set the iTerm titles to final part of the current working directory and the iTerm window title to be the full path of current tab.


Start a new terminal window or tab after making the following changes.  New tabs and iTerm windows create new login sessions that read these file contents.

Modify ~/.bashrc

  1. Edit  ~/.bashrc.  Create ~/.bashrc if it doesn't exist.
  2. Add the following text to the file.  Note that this text has comments that document where I found this on the internet

# Set iTerm2 tab titles to the last directory in PWD
tabTitle() { echo -ne "\033]0;"$*"\007"; }
# Set iTerm2 win titles to the full directory of PWD
winTitle() { echo -ne "\033]2;"$*"\007"; }

# Alias 'cd' to list directory and set title
cd() { builtin cd "$@"; ls -lFah; tabTitle ${PWD##*/}; winTitle ${PWD/#"$HOME" /~}; }

Modify ~/.bash_profile

Mac terminal emulators start a login session for every new tab.  This means we need update ~/.bash_profile to invoke ~/.bashrc
  1. Edit ~/.bash_profile.  Create ~/.bash_profile if it doesn't exist.
  2. Add the following text to the file.  Note that this text has comments that document where I found this info on the internet.
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi 

No comments:

Post a Comment