Dynamic terminal window titles

May 22, '01 02:53:50AM

Contributed by: cft

I learned about this in college to make the title of xterms dynamic and found that the same method works with Terminal.app. I've created some aliases that allow me to have the title of the window reflect the machine that I'm on and current working directory of the shell, or of what file I'm editing. If you'd like a title on your terminal window that changes based on what you're doing, read the rest of this article...

The title in my example only contains the name of the current directory and the parent directory (so if I was in my Documents directory on my laptop called greyhound, the title would look like:

greyhound: cft/Documents
I also use the same technique for my prompt so that when in real deep directories, my prompt does not go totally across the window. I have included a sample alias called setPrompt2 that does this. When editing a file with vi, it would look like:
greyhound: vi file_name
Please note that if you suspend a vi session, it will not change the title back, that will only happen when vi exits (or is killed). Changing directories with cd will always update the title bar.

Here are the aliases:
alias settitle 'set t=$cwd:h;echo -n "^[]2;${HOST}: "!*"^G"'
alias setPrompt 'settitle $t:t/$cwd:t'
alias cd 'cd !*;setPrompt'
alias vi 'settitle "vi !*"; vi !*; settitle $t:t/$cwd:t'
setPrompt
alias setPrompt2 'set t=$cwd:h;set prompt="${USER}@$HOST $t:t/$cwd:t !> ";settitle $t:t/$cwd:t'
Please note that in the alias settitle there are two control characters, ^[ and ^G. Control characters do not copy and paste correctly so you will need to edit them by hand. To enter them you need to use vi and type control-v then then the control character that you want (control-[ and control-G). Emacs should work too, but I do not know how to enter them in emacs.

For those of you who are not comfortable with doing that, I have put the aliases in a file called settitle in my iDisk, here is the link: http://homepage.mac.com/cftarnas/FileSharing.html

In order to get these to work the file needs to be sourced by the shell. That can be done manually by typing entering the command:
source settitle
This assumes the aliases are in a file called settitle. To make it automatic whenever you start a new shell, put them in a file called .tcshrc in your home directory.

[Editor's note: As others have noted on other hints about aliases, you should actually store these in a file called aliases.mine in the path ~/Library/init/tcsh (you may need to create the init and tcsh directories first).]

Chris Tarnas

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20010522025350220