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

Set terminal title to current directory UNIX
Want to display your hostname and current directory in Terminal.app's title bar? Now you can!
  1. Create a script called chg_dir.sh (/Library/Scripts is a good place) with contents as follows:
    #!/bin/sh
    dir=$1
    if [ ! ${dir} ] ; then
    dir="$HOME"
    fi
    echo -n "^[]0;`uname -n`:${dir}^G"
    NOTE: ^[ represents the escape key, ^G represent control-G. To type these non-printing characters in vi, use ctrl-V and then "escape" and "control-G".

  2. Edit your .cshrc (or equivilant) and add these two lines:
    echo -n "^[]0;`uname -n`:${HOME}^G"
    alias cd "cd !* ; /Library/Scripts/chg_dir.sh !*"
Now when you start a new terminal session, the title is set to "host:/home/dir". When you change dir it becomes host:/current/dir".

[Editor's note: There was another way to accomplish this same result posted in an earlier hint, but this one was unique enough to merit its own mention. I tested this, and it works as described. I added the two lines to my existing "aliases.mine" file, and placed the script in ~/bin. I still personally prefer this hint, which explains how to show the current command in the title bar, but the directory info is also useful.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[13,879 views]  

Set terminal title to current directory | 4 comments | Create New Account
Click here to return to the 'Set terminal title to current directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Why use a script?
Authored by: a1291762 on May 02, '02 07:39:35PM

> Create a script called chg_dir.sh

Why use a script? Every time you do a 'cd' you have to start a new instance of /bin/sh. Sure it's not a slow thing but it adds just a bit of time to every cd you do.


I would do something like this: (replace <backslash> with a backslash)

alias cd 'chdir <backslash>!*; set prompt="%{<backslash>033]0;[%~]<backslash>007%}[%~]%# "'

chdir is a tcsh build-in that works the same as cd.
<backslash>!* says "put all the arguments to the alias here".
The [%~] part is what goes in the title bar.
Everything after the last } is your normal prompt.
The %# is supposed to change the prompt char to # when you are root. Unfortunately, I don't think it works with "sudo -s".
The default prompt has %u (I think) which puts in your effective username so that would work.

With the above alias, if you type 'cd ~/bin' you'll have a title of '[~/bin]' and a prompt of '[~/bin]> '.



[ Reply to This | # ]
Why use a script? -hostname only?
Authored by: baba on May 02, '02 08:42:35PM

Can this be done so that only the hostname displays in the title?



[ Reply to This | # ]
Re: Why use a script? -hostname only?
Authored by: sjk on May 02, '02 09:30:36PM
Generalized tcsh example:

alias xtitle printf '"\e]2;\!*\a"'

[There are backslashes before the 'e', '!', and 'a' characters in the argument to printf; this avoids using non-printable characters in the alias]

alias cwdcmd xtitle '$cwd'

Replace the argument to xtitle with whatever you want displayed in the Terminal title bar when changing directories in the shell, like:

alias cwdcmd xtitle '`dirs`'

Run:

unalias cwdcmd
xtitle anything

... to set the title bar to something that doesn't change.


[ Reply to This | # ]
Why use a script? -hostname only - success
Authored by: baba on May 03, '02 11:08:22AM

Ah, silly me. I got it to work by specifying machine name:
alias cd 'chdir <backslash>!*; set prompt="%{<backslash>033]0;%m<backslash>007%}[%~]%# "'
so now I have the host in the window and the current path at the prompt.
Most exellent.
The big question now is can I set the terminal background color according to the host??? Right now I keep a bunch of saved term sessions which specify colors for certain hosts. I'd rather it be automatic.



[ Reply to This | # ]