MacOS – How to set the terminal tab title via command line

command linemacosterminal

I want to set the terminal tab name from the command line. I am using this command to update the name echo -en "\033]0; New Name \007". Here is what my terminal now looks like.

New Name with other stuff

When I use the Inspector through GUI, I have the ability to set the terminal name and the tab name. When I set it via the GUI, here is what I see.

New Name alone

As you can see there isn't any dashes - or text on the tab. How do I do this from the command line?

Best Answer

TLDR

You can do this with something like echo -en "\033]1; New Name \007" or printf '\e]1;%s\a' 'New Name'. You may have to fiddle with your profile's window/tab title settings, though.

Explanation...

This behavior is a little complex and not well explained I guess.

Title types

First, you can set two types of title. Terminal.app calls these the tab title (aka the icon name/title) and the window title (as you can see in the inspector).

The XTERM escape sequences for setting these are:

  • ESC]0;stringBEL -- Set icon name and window title to string
  • ESC]1;stringBEL -- Set icon name to string
  • ESC]2;stringBEL -- Set window title to string

I'm not sure how common this behavior is across terminal emulators, but Terminal.app doesn't actually do what 0 describes here. It sets the window title, and clears the tab title (probably because, as I'll show later, Terminal will pick up the window title if the tab title is unset).

The reason you see different behavior is because your command is setting the window title, but in the inspector you're setting the tab title.

Terminal.app title features

The dashes in the title are just separators that Terminal.app is using whenever it's combining multiple pieces of information in your title. (Pieces here include the profile "tab" and "window" Title options, as well as the window and tab titles themselves)

If you set both a window title and a tab title, it'll use them both. With multiple tabs, it'll separate them:titles with tabs

If the tab is in it's own window, though, it'll combine them and show the separator (unless you use the view menu to force it to show the tab bar even with a single tab):titles without tabs

You can still get it to show a single title, but you'll have to use only the window or the tab title (the window title will pick up the tab title if no windows title is set):windows with tab & window titles

The inverse (tabs can pick up a window title if no tab title is set) is also roughly true, but the behavior can be a little surprising. Here are the same tabs in two windows along with the settings panel (to show that it's set to show the TTY name, but not to show items when there's a custom title): tabs with tab & window titles The top window is picking up the default "Terminal" title because the current tab doesn't have a window title. Its tab doesn't show the TTY, because it has a custom tab title. The bottom window shows that the tab will pick up the window title if it doesn't have a tab title. The TTY shows up in the tab title because it doesn't have a custom tab title--just a custom window title.