39

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.

  • Should note that there's nothing OS X specific about this -- the answer will work on pretty much any terminal. – Andrew Aylett Feb 6 '10 at 11:22
  • Is it Bash-specific then? – Dan Rosenstark Feb 6 '10 at 14:55
  • @Andrew Aylett, so the script I put in my answer (below) will work on the linux distros, too? – Dan Rosenstark Feb 6 '10 at 18:20
  • Yes, it should work anywhere (with an sh-derived shell) that uses unix-y terminals. – Andrew Aylett Feb 11 '10 at 22:02
48

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.

|improve this answer|||||
  • 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 6 '10 at 9: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 6 '10 at 12:03
  • 1
    I used $* and within a function, so I wouldn't have to quote my title string. – BeepDog Jul 1 '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 2 '15 at 17:23
  • OK: Just fprintf(stdout,..) works. I should have tried before asking. – phs Nov 3 '15 at 6:52
35

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.

|improve this answer|||||
  • why is it a problem if you have many functions in .profile? is there a limit? – tgkprog Jun 4 '13 at 14:22
  • 1
    IDK why, but had to edit .bash_profile instead of .profile – Nakilon Sep 8 '13 at 23:45
  • 2
    Or: alias title="printf '\033]0;%s\007'". – kenorb May 14 '15 at 12:40
  • export alias title="printf '\033]0;%s\007'" – Mwayi Apr 27 '17 at 9:55
  • You can also add it using sudo nano /etc/bashrc. This should run for all the users. Restart terminal session or do source /etc/bashrc to apply your changes. – Eugene Kulabuhov Jun 26 '17 at 18:50
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)

|improve this answer|||||
  • If you're on one of those newer versions of OS X you may to do /usr/local/bin/title instead. – Samy Bencherif Jun 9 '17 at 3:02
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.

|improve this answer|||||
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.

|improve this answer|||||
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]"
|improve this answer|||||
0

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.