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

Dynamic terminal window titles UNIX
I learned about this in college to make the title of xterms dynamic and found that the same method works with 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'
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:

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
  • Currently 4.00 / 5
  You rated: 4 / 5 (3 votes cast)

Dynamic terminal window titles | 7 comments | Create New Account
Click here to return to the 'Dynamic terminal window titles' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: babbage on May 22, '01 10:29:57AM
I can't get the material you have on here to work. There seems to be at least one typo -- you've got "settile" on the end of the last line instead of "settitle" (missing the 3rd T). Since you've put it online anyway, it's easier for readers to just download that: wget That is working just fine for me...

[ Reply to This | # ]
alias for ssh to change title?
Authored by: to0l25 on Oct 18, '01 08:57:55AM
that is definitly the only one that works for me, thanx alot.
unfortunately i often work via ssh (with ssh -l username hostname) on other hosts, so it would be useful to have the remote host in the title bar. i tried hard but i didn't get it right for my purposes.
i do it this way in the moment, but i am not really satisfied with it, 'cause it's not flexible enough: i need to have an alias for every host to connect:

alias ssh_host 'settitle "ssh on host.tld"; ssh -l user host.tld; settitle $t:t/$cwd:t'

this will set the title to "ssh on host.tld", but i do not have the path i am working in. i also changed the first line and delete ${HOST} (if i didn't do that the output for my ssh-alias was a bit confusing). maybe someone is able and willing to help me out. thanx in advance, to0l25

[ Reply to This | # ]
Any way to get this to work when telnetting elsewhere?
Authored by: sjonke on May 22, '01 03:04:27PM

I tried using this, and on my mac itself this works fine, but I was hoping it would update when I telnet to another machine and it doesn't. Is there any way to get that to work? Specifically, that is the only thing I'd want the title to show, the host you are currently on.


[ Reply to This | # ]
Any way to get this to work when telnetting elsewhere?
Authored by: robbchar on May 23, '01 07:59:16PM

I assume that most of this can be done in the .profile of your account on the box your a telnetting into. current my prompt is set locally by this:

MACHINE=`uname -n`; export MACHINE

and that updates with whatever directory. Eventually I will get around to just chopping off the last directory with regular expressions (at least I think thats how its done) but I havent had time. I'm not completely sure, but I bet the title of the window could be done that way too, maybe. Or you could use an alias to do it whenever you telnet, something like this:

alias telnet 'settitle "telnet !*"; telnet !*; settitle $t:t/$cwd:t'

you could make it change the title with any app that you run in that manner. I hope that helps, most of this I havent tried, but it seems like it would work.

good luck


[ Reply to This | # ]
Entering control characters with emacs
Authored by: Cadre on May 23, '01 06:14:25PM

To enter control characters with emacs, precede it with C-q (control q)

[ Reply to This | # ]
An simpler alternative for bash
Authored by: melquiades on May 23, '01 11:41:48PM
If you're using bash, you can just include the title-changing code in your prompt (I put this line in my .profile): PS1="\[^[]2;custom-window-title^G\]custom-prompt" For example, I use the following: PS1="\[^[]2;\u@\H (\W)^G\]\h (\W)\\$ " There are a bunch of codes you can use in your prompt (copied from the bash man page): d the date in "Weekday Month Date" format h the hostname up to the first `.' H the hostname j the number of jobs currently managed by the shell l the basename of the shell's terminal device name n newline r carriage return s the name of the shell, the basename of $0 (the portion following the final slash) t the current time in 24-hour HH:MM:SS format T the current time in 12-hour HH:MM:SS format @ the current time in 12-hour am/pm format u the username of the current user v the version of bash (e.g., 2.00) V the release of bash, version + patchlevel (e.g., 2.00.0) w the current working directory W the basename of the current working direc- tory ! the history number of this command # the command number of this command $ if the effective UID is 0, a #, otherwise a $ nnn the character corresponding to the octal number nnn a backslash I'm sure there's an analogous way for tcsh.

[ Reply to This | # ]
An simpler alternative for tcsh
Authored by: leo on May 24, '01 04:54:08PM

In tcsh you do not need to modify the prompt in order to change the window title.
You may use an alias called cwdcmd. tcsh will execute this alias whenever the cd
command is executed. Here are two aliases to accomplish this task.

alias settitle 'echo -n "^[]2;"!*"^G"'
alias cwdcmd 'settitle $cwd'

I personally use the following command, which will do an ls and change
the window title everytime I move to a new directory.

alias cwdcmd 'ls ; settitle $cwd'

and here is an example to modify the prompt using an environment variable.
Now your promt will always contain the login name and the trailing component
of $cwd.

set prompt="$USER[%c]: "

You may get the above mentioned commands by downloading files aliases.mine and
environment.mine from my shared folder (copy them to ~/Library/init/tcsh).

[ Reply to This | # ]