Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to set the current Terminal title UNIX
I've seen many hints to change the shell prompt but changing the Terminal window title from the shell I don't remember seeing here before. So I wrote a little shell script that does just that.

For example, to change the Terminal's title to 1337 H4X0R with the script saved as title, you'd type title "1337 H4X0R" at the prompt.

I use it to name alternate shells when I invoke them:
alias bash 'title bash;/usr/local/bin/bash'
alias zsh 'title zsh;/bin/zsh'
Read the rest of the article for the script.

The script:
#! /bin/sh
help0()
{
echo "ERROR: No argument provided."
echo "Usage:"
echo " `basename $0` \"title\" to provide a new Terminal title."
}

help2()
{
echo "ERROR: Too many arguments provided."
echo "Usage:"
echo " `basename $0` \"title\" to provide a new Terminal title."
}

if [ $# -eq 0 ]
then
help0;
elif [ $# -eq 1 ]
then
osascript -e "tell application \"Terminal\" to set
custom title of window 1 to \"$1\""
elif [ $# -gt 1 ]
then
help2;
fi
NOTE: The "osascript" line has been broken onto two rows; enter as one with a space where the line break currently exists! Save the script as a text file and move it to somewhere in your path, ~/bin is a good place. Remember to make the script exectuable (chmod 755 script_name) and then type rehash to start using it right away.
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[38,577 views]  

A script to set the current Terminal title | 12 comments | Create New Account
Click here to return to the 'A script to set the current Terminal title' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
And For Your Remote Server...
Authored by: thinkyhead on May 24, '02 04:12:41AM

Replace the call to OSASCRIPT with this line in BASH:

<b>echo -e "\033]0;$1\007"</b>

or this line for CSH, ZSH, etc.:

<b>echo -n "^[]0;$1^G"</b>

Note that ^[ is actually the <b>esc</b> character and ^G is <b>ctrl-G</b>.To enter these characters use <b>vi</b> and use the following sequences for each:

^[ ........ <b>ctrl-v - esc</b>
^G ...... <b>ctrl-v - ctrl-g</b>

Now when you connect to your remote host you can have your .login or .bashrc file set the Terminal title to the name of the remote server.

In tchs this command will make the Terminal title always show the current directory:

<b>alias precmd 'echo -n "^[]1;$PWD^G"'</b>

Use the same trick for esc and ctrl-G.



[ Reply to This | # ]
(better in HTML mode)
Authored by: thinkyhead on May 24, '02 04:14:50AM
Replace the call to OSASCRIPT with this line in BASH:

echo -e "

[ Reply to This | # ]
(damn geeklog!!)
Authored by: thinkyhead on May 24, '02 04:16:58AM
Replace the call to OSASCRIPT with this line in BASH:

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

or this line for CSH, ZSH, etc.:

echo -n "^[]0;$1^G"

Note that ^[ is actually the esc character and ^G is ctrl-G.To enter these characters use vi and use the following sequences for each:

^[ ........ ctrl-v - esc
^G ...... ctrl-v - ctrl-g

Now when you connect to your remote host you can have your .login or .bashrc file set the Terminal title to the name of the remote server.

In tchs this command will make the Terminal title always show the current directory:

alias precmd 'echo -n "^[]1;$PWD^G"'

Use the same trick for esc and ctrl-G.

[ Reply to This | # ]
Geeklog and slashes...
Authored by: robg on May 24, '02 08:04:11AM

The good news is that this has apparently been greatly improved in the newest version. Even better, they're close to releasing it and I have a version running locally ... so we will be upgrading very soon now!

Thanks for putting up with it so far; it's incredibly annoying, I know!

-rob.



[ Reply to This | # ]
Geeklog and slashes...
Authored by: thinkyhead on May 27, '02 04:30:25AM

(Psst... It's me, I'm the guy who supplied the patches to the geeklog and phplib projects at sourceforge. The backslash problem should be completely solved by the upcoming update to these two parts of GeekLog. The major cause of the problem was with phplib, and not geeklog per se.)



[ Reply to This | # ]
Easier in the GUI
Authored by: BobWestfield on May 24, '02 10:43:11AM

You can do this in the GUI.

Open the terminal.

Go to File > Preferences.

Click the Window icon at the top of the preferences pane.

Click "Custom Title" and enter whatever you want the title to be in the terminal window.

-Bob



[ Reply to This | # ]
GUI easier, command line faster
Authored by: robg on May 24, '02 10:50:56AM
But if you want to quickly set the title of a window, it's hard to beat title foobar for raw speed! And if you often set window titles, the speed savings will be greatly magnified by using the script.

Personally, I use the other hints that have been posted here to just dynamically set the window title to the current command ... works great for me!

-rob.

[ Reply to This | # ]
Previously Covered...
Authored by: CyborgSam on May 24, '02 02:05:14PM

in:

http://www.macosxhints.com/article.php?story=20010522025350220

I've been using that hint and like it. It changes the window title when using vi, I have not modified it yet for pico, which I use more than vi...



[ Reply to This | # ]
Previously Covered 2
Authored by: Viridian on May 24, '02 07:53:27PM

As noted by CyborgSam (who was kind enough to provide the link), this was previously covered. Read all the replies for excellent advice, in particular a single line that does essentially the same thing as the entire script:

alias settitle 'echo -n "^[]0;\!*"^G'



[ Reply to This | # ]
Use 'precmd' and 'postcmd' in tcsh
Authored by: Viridian on May 24, '02 09:29:02PM

robg has stated his preference for an alias that puts the current job in the title bar using the 'postcmd' variable, but when I put directory info in the alias, I noticed that when I changed directories, it would display the directory I had just left, not the one I was in, until I entered another command or pressed Return. After much experimentation, I found that setting both the 'precmd' AND 'postcmd' variables did what I wanted.

alias precmd 'echo -n "^[]0;"`date "+%H:%M:%S"`" ${cwd}^G"'
sched +0:00 alias postcmd 'echo -n "^[]0;`date "+%H:%M:%S"` ${cwd} => \!#^G"'

[Note: there are three spaces for separation after the back-quoted 'date' command in both aliases.]
They both echo the current time (as a time stamp) and working directory, but the 'postcmd' alias also echoes the current job. Here's an example of what they do:

When a Terminal window is opened, the title bar displays the time and home directory:

19:04:21 /Users/<username>

Navigate to ~/Library/init/tcsh:

19:07:41 /Users/<username>/Library/init/tcsh

Edit aliases.mine:

19:11:32 /Users/<username>/Library/init/tcsh => pico aliases.mine

What's interesting is that all current processes are displayed in the title bar while they are being executed, so on a slow Mac you can make out entire scripts flashing by before your eyes. Talk about dynamic!

On an unrelated note (well, it's about aliases so it's somewhat related), I also use the 'cwdcmd' special variable, which executes on a directory change. I set it to display a brief message and produce a color listing of the directory contents.

Here's how: put the following Perl script (mine is named "cwdmessage") in ~/bin and set its permissions to executable.

#! /usr/bin/env perl

print ("\n\033[37;100mCURRENT DIRECTORY:\033[0m ") ;

Then add these lines to ~/Library/init/tcsh/aliases.mine

alias lx 'ls -F --color' # Assuming you have 'color ls' installed.
alias lc 'ls -GAhlk --color | more' # 'man ls' for options.
alias cwdcmd 'cwdmessage ; cwd ; ls --color' # Put your preferred 'ls' alias here.

Why did I use a Perl script instead of 'echo' ? Because I discovered by accident (complete newbie here) that Perl recognizes ANSI color tags, so I set the message to a different color than normal text. When I change directory, a line is skipped and the message "CURRENT DIRECTORY:"(in white on a dark grey background) is displayed, followed by the directory name and a color listing of its contents.
See my tip about ANSI color codes.

http://www.macosxhints.com/article.php?story=20020517085243183

The tcsh man pages are a wealth of information about special variables, and much, much else.[Tip: in the man page type '/' to enter search mode and enter the string you want. Press ctrl-n to skip to the next occurence.]



[ Reply to This | # ]
Use 'precmd' and 'postcmd' in tcsh
Authored by: liyanage on May 27, '02 03:44:22PM

Wow this stuff is awesome, exactly what I always wanted!!

I removed the date as I don't need that, but I prefer to see the "~" sign in place of my home directory in the path as that makes the string a bit shorter. So I changed the line to:

alias precmd 'echo -n "^[]0;${cwd}^G" | sed -e "s%$HOME%~%"'

I noticed however that sometimes newly opened terminal windows close again immediately. When that happens, I have to hit Cmd-n again, sometimes twice or three times... Anybode else seen this?



[ Reply to This | # ]
ssh hostname in title
Authored by: michelj on Oct 09, '09 03:53:35AM

I'm a sysadmin of many servers, and I like keep a lot of terminal windows open. I set the hostname automatically in each window title.

Here is what I've done ( works in 10.5 and 10.6 ):

create the file /usr/bin/ssh.title:
#!/bin/bash
function settitle() { echo -ne "\033]0;$@\007"; }
settitle 'ssh' $1
/usr/bin/ssh $1 $2 $3 $4

make it executable:
chmod 755 /usr/bin/ssh.title

In the "New Remote Connection" window of terminal, add a service (click the left '+' sign)

Service Name: ssh
Bonjour Name: _ssh._tcp.
Command to execute: /usr/bin/ssh.title
Check "Passes username to command"

"ssh" will then appear in the service column. Each ssh window will show the remote hostname in the title.



[ Reply to This | # ]