43

How can I arbitrarily change the title of a Terminal window in Mac OS X? I have seen this question and this magicwrap thing, but think it's just a simple Mac OS X command.

Dan Rosenstark
  • 6,388
  • 13
  • 59
  • 95

7 Answers7

52

This article tells you how.

Essentially, you use character sequences echoed to the screen to inform the terminal of what title it should display.

title='My first title'
echo -n -e "\033]0;$title\007"

In the above example, whatever the variable title is set to while become the terminal's title. Of course, you could just have the title in the string to echo such as:

echo -n -e "\033]0;My first title\007"

But the first way makes it a slightly bit easier to use and/or extend later.

Dan McGrath
  • 2,996
  • 22
  • 20
  • Excellent. Could you include a sample script (like this? `echo -n -e "\033]0;$1\007"`) in your answer so I can mark it best answer, please? – Dan Rosenstark Feb 06 '10 at 09:15
  • 2
    *printf* may be more reliable: `printf "\033]0;%s\007" "$title_variable"` (the various options and behaviors of *echo* are not the same across all systems, shells, or even shell options) Also, variable assignments in *bash* should not have spaces around the equals sign. – Chris Johnsen Feb 06 '10 at 12:03
  • 1
    I used $* and within a function, so I wouldn't have to quote my title string. – BeepDog Jul 01 '15 at 21:39
  • Is there a way to change the title from within a C program? The program I'm thinking about is running animation based on ncurses.h so I am not sure how to echo to the terminal :-( – phs Nov 02 '15 at 17:23
  • OK: Just fprintf(stdout,..) works. I should have tried before asking. – phs Nov 03 '15 at 06:52
  • what about tab name – Muhammad Umer Jun 02 '16 at 15:11
38

Adding the following to your ~/.profile will achieve the same effect:

# function for setting terminal titles in OSX
function title {
  printf "\033]0;%s\007" "$1"
}

And then a quick title 'et voila' will sort all your tabs out.

techpeace
  • 101
  • 2
  • 4
8

Remix of Dan MgG's answer:

echo -n -e "\033]0;$1\007"

Store it in a file called /usr/bin/title (using sudo!) and chmod it to +x. Then from anywhere you can just type

title 'Trying to Figure This GIT Thing Out'

and you get a nice little title.

(Syntax may vary if you're not on OSX, if I understand correctly)

Dan Rosenstark
  • 6,388
  • 13
  • 59
  • 95
2

As an alternative to sh-based command line solutions, the OS X Terminal app has a preference to change the title as follows: Under the Terminal->shell menu there is a "edit title" choice, select that and you can change the title easily.

fideli
  • 14,636
  • 2
  • 35
  • 44
user572813
  • 31
  • 1
2

The reply marked as Best answer works fine... this is what i did...

tell application "Terminal"
    activate
    do script "echo -n -e \"\\033]0;WorkerTab1\\007\"; cd $HOME/folder1"
end tell

this will set the name of the new tab to WorkerTab1 and then perform other commands like "cd" , etc.

Bates
  • 21
  • 1
1

On OS X, terminal preferences are stored in ~/Library/Preferences/com.apple.Terminal.plist.

The terminal's title is stored in the WindowTitle preference.

J.Money
  • 111
  • 3
0

Thanks for this. I just added a function to my .bashrc:

    function stit() {
    echo -n -e "\033]0;$1\007"
}

In my mind "stit" = a convenient shortcut for "set_title". And now when I want to set the title of my windows on the fly, I type:

stit "[new window title]"