Changing tab titles in the macOS Terminal app using the command line
Published on 25th December 2024 Estimated Reading Time: 2 minutesOne thing that I have noticed with the macOS terminal app that I have not seen with its Linux counterparts is that the tab titles can get stuck after an SSH session to a remote server. Thus, I decided to see if they could be changed or reset. Handily, a single command will do just that:
echo -ne "\033]1;New Tab Title\007"
In a UNIX shell (BASH, ZSH, etc.), the echo
command outputs text, and it is the text that changes a tab title. Here, the -ne
options both negate the generation of a newline (which would be the function of the -n
switch if used on its own) and interprets the escape characters included in the text (which would be the function of the -e
switch if used on its own).
Within the text string \033
is the octal representation of the escape character that initiates the control sequence that follows it. This is ]1;
, the Operating System Sequence (OSC) for setting the tab title in this case, more generally the icon and window title in other circumstances. The text New Tab Title should be self-explanatory, while \007
is the octal representation of the bell character (BEL) that terminates the OSC.
Because I wanted to have the current working directory path as the title, I made a small modification to do this dynamically:
echo -ne "\033]1;$(pwd)\007"
It is the $(pwd)
portion that does just that, taking the output of the pwd
command and adding it into the string. Thus, I see what is open in each tab. That stopped me ending up in the wrong one, and I even added an alias into the .zshrc
file to make it easier to invoke. The functionality may be a more general UNIX or Linux feature, though I have not had opportunity or reason to try it just yet.