15

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?

4
  • I don’t see a pure duplicate, but maybe check this or that. Also, what precisely do you want to show since you passed a string without dashes I’m not sure where you want the dashes to show.
    – bmike
    Commented Jul 17, 2019 at 17:23
  • In terminal.app go to preferences > profiles. You can set the options there. Commented Jul 17, 2019 at 19:01
  • 2
    I am trying to do this all programmatically. I don't want to use the GUI.
    – jhamm
    Commented Jul 18, 2019 at 2:27
  • 1
    I just want to add a title to the tab and nothing else. Then I can see exactly what I am doing on that tab. The dashes are other stuff are just noise and get in the way.
    – jhamm
    Commented Jul 18, 2019 at 2:28

1 Answer 1

24

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.

6
  • 1
    If you're using bash, watch out for how your PS1 is set. I somehow have $debian_chroot embedded in PS1 and use PS1 to reset my xterm title every time my bash prompt is shown - completely screwed me up. Commented Mar 8, 2022 at 2:11
  • This seems like the most intelligent answer to the question... but it's not working for me (macOS Ventura 13.6+). All I want to do is what's shown in Terminal, Settings, Profile, Tab under the "Show other items when there is a custom title", under the "Escape sequence": The following example shell command sets the tab title to “My Tab”: printf '\e]1;%s\a' 'My Tab'. I type this into the command line on the tab I want to change - but nothing happens. Could you possibly expand on your answer here; esp wrt "You may have to fiddle with your profile's window/tab title settings, though."?
    – Seamus
    Commented Sep 1 at 20:49
  • @Seamus Hard to know, but my first guess would be that, as tdwong.star noted, you may have a PS1 (or perhaps a PROMPT_COMMAND) that sets the title every time you run a command. I'd try to right-click in that terminal to open the inspector, manually change the title in the inspector, and then see if it reverts when you run a command?
    – abathur
    Commented Sep 3 at 19:59
  • Yes - the Inspector settings for 'tab' & 'title' revert immediately after entering any command under that specific 'tab' (all other tabs are unchanged until I have to type a command in them). WRT tdwong's cmt: I'm using (native) zsh - not bash. My PS1 is not set in ~/.zshrc - I use something called pure
    – Seamus
    Commented Sep 3 at 20:13
  • I'm not as familiar with zsh, but I think you can still use set -x (enable trace mode) to see what's running, and then maybe search through the output of set for something that matches. It could be part of pure, or perhaps something Apple puts in the stock system-wide /etc/zshrc and/or /etc/zshrc_Apple_Terminal
    – abathur
    Commented Sep 3 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .