How to change the Mac Terminal title from the command line

Mac Terminal FAQ: How can I change the title on the macOS Terminal app from the Mac/Unix command line?

I've been working on a project where I have three Mac Terminal tabs open at one time, and I found it was much easier to work this way when I changed the title on each Terminal window. This helped me easily identify what I was doing in each Terminal window.

Changing the Mac Terminal title

The basic escape sequence you need to change the Terminal title from the command line is this:

echo -n -e "\033]0;YOUR TITLE HERE\007"

When you issue this command from the command line of a Mac Terminal window, it will change the title in the Terminal's title bar to "YOUR TITLE HERE."

In the real world ... what I did was label one of my Terminal windows as "CLIENT", one as "SERVER", and the other as "BUILD". I did this by issuing commands like this:

The Terminal title for window #1:

echo -n -e "\033]0;CLIENT\007"

Terminal title for window #2:

echo -n -e "\033]0;SERVER\007"

Terminal title for window #3:

echo -n -e "\033]0;BUILD\007"

Note that you can also create a Unix shell script to make this title process easier, i.e., so you can just type something like maketitle "SERVER".

Bonus: Set the Terminal title to the current directory

If you want to set the Terminal title to the name of the current directory, do two things:

  1. Create a shell script named settitle to do the things I just showed
  2. Create a Unix alias (or shell script) to set the Terminal title to the current directory, using settitle

As a quick example of this, here’s the contents of my settitle script:

#!/bin/sh
echo "\033]0;${1}\007\c"

And here’s an alias I use that sets the Terminal title to the name of the current directory:

alias settitlePwd='settitle $(basename `pwd`)'

So, if I want to change the Terminal title to “SERVER”, I type this:

$ settitle SERVER

But if I’m currently in a directory named /Applications/MAMP/htdocs/aad828/themes, and I want to set the Terminal title to themes, I use this command instead:

$ settitlePwd

Give those commands a try on your system, see if you like them, or change them however you see fit. The important part for me is getting the macOS Terminal title to be whatever you want it to be, in an easy manner.