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

Set the title of the Terminal window with cd UNIX
Terminal lets you set the window title with some interesting options, but alas not the current directory. I wanted to move the current directory from the prompt to the window title. The downside is that you cannot copy the current working directory without typing pwd, but there are several upsides.

First there is the settitle function, which someone else wrote:
function settitle() { echo -ne "\e]2;$@\a\e]1;$@\a"; }
This changes the title of the current window. In Terminal, this is the string you can set in the Window Settings that defaults to "Terminal" but not the whole window title.

Next there is a new cd function to replace the built in command:
function cd() { command cd "$@"; settitle `pwd -P`; }
Using command before cd forces bash to use the built-in instead of the function, so no infinite loop on cd. You could pass anything to settitle; this passes the current full path without links.

To make this complete, change the prompt to remove the redundant path:
default: '\h:\w \u\$ '
export PS1='\h:\W \u\$ '
export PS1='\h:\u\$ '
The default is to have the full path after the hostname. The first option reduces the full path to the name of the current directory. The second option removes the path altogether.

Finally, if you want to be consistent, add the following line somewhere so that when the shell starts the title is set. Otherwise the title will be "Terminal" or whatever is set in the Window Settings until cd is called. Of course, you may prefer that.
settitle `pwd`
There are a lot of variations on how the path is derived and displayed, and what other information would be useful in the window title. Here is one last example that more closely matches the information in original prompt:
"${HOSTNAME%%.*}:${PWD/$HOME/~} $USER"
I assume that most people who would care about this also know where to place the above commands. But just in case, stick all the above code in either .bashrc or .profile in the home directory.

[kirkmc adds: We've run other hints on changing Terminal window titles, including this hint and this hint, both of which give different methods for putting the current directory in the window title. This is another way, and may interest some readers just because it's different.]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[29,110 views]  

Set the title of the Terminal window with cd | 11 comments | Create New Account
Click here to return to the 'Set the title of the Terminal window with cd' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set the title of the Terminal window with cd
Authored by: fresler on May 04, '06 07:43:28AM

After reading this hint, I went to my terminal and discovered that the path was already in the title bar. However, I had never done this hint or the other two mentioned. I then discovered that during local access, the title bar didn't change as I changed directories. However, if I SSHed into a remove server, the title bar changed to the user I was logged in as and the current directory. It looks somethink like this: "username@server:/home - ssh - 100x31". I'm not sure if this the default behavior of the terminal or if I applied some other hint I ran across. If I did, I sure can't remember it!



[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: enderai on May 04, '06 09:58:06AM

I noticed this too one day. Look at your .bashrc on the remote server, and you'll find some commands that set the window title of an xterm session to user@localhost:/path. Well, when Terminal.app is set to emulate an xterm session, it picks up the window title change, too. The same code works if you copy it into your .bash_profile or .bashrc file on your Mac.



[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: denny on May 04, '06 07:41:12PM

PS1='\[\033]0;\u@\h:\w\007\]\u@\h:\w) '


I use this line in my .bash_login file. Have used that for years and it works pretty nicely for me.



[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: adrianm on May 04, '06 09:13:25AM

This method is useful in many different terminal programs, including PuTTY on Windoze.



[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: vdanen on May 04, '06 09:57:44AM
For those using zsh, you can do the same thing by editing ~/.zshrc and adding:

# this is used in zsh instead of PROMPT_COMMAND in bash:
precmd() {
  print -Pn "\e]0;%n@%m: %~/\a"
}
Then you don't have to monkey with aliasing cd and constantly running pwd. And if you put the same thing onto a remote machine (or the bash trick outlined above or any of the variants) you get the user@remote [path] stuff in the title bar as you navigate around on remote machines too.

[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: boredzo on May 04, '06 10:13:02AM

There's also preexec, which is run before running any command in the shell. I use preexec to add the process arguments to my title bar, and precmd to take them out (by resetting to just the CWD and TTY).

From my .zshenv:

if [[ ( "$TERM" = "xterm-color" ) || ( "$TERM" = "xterm-256color" ) ]]; then
	#When a program starts, put it in the title bar.
	preexec () {
		print -n "\e]0;"; print -Pn "$1 @ %~ @ tty%l\a" | python ~/Python/newlineescape.py
	}
	#When a program exits, remove it from the title bar.
	precmd () {print -Pn "\e]0;%~ @ tty%l\a"}
fi

The Python script newlineescape.py takes out control characters in the arguments that could mess up my title bar and terminal and cause beeping (anything after the first line will be printed to stdout). It's pretty simple:

import sys
sys.stdout.write(' '.join(sys.stdin.read().split()))



[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: jacobolus on May 04, '06 02:24:04PM
Why not just change the titlebar in your prompt? I use:
titlebar='\[\e]2;local: \w\a'
blue='\e[0;34m'
red='\e[0;31m'
NC='\e[0m'
export PS1="\[${titlebar}${blue}\]local: \w \[${red}\]\$\[${NC}\]"
But to set it up somewhat similar to this hint, try:
titlebar='\[\e]2;\w\a\e]1'
export PS1="\[${titlebar}\]\h:\u\$ "
Then it will get executed every time the prompt shows up. Much better than making odd custom cd commands.

[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: jacobolus on May 04, '06 02:56:55PM
Note: you want to put those lines in your ~/.bashrc

[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: jwbales on May 04, '06 10:29:19PM
Why not just set PROMPT_COMMAND in your .bashrc?
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}: ${PWD/#$HOME/~}\007"'
If $PROMPT_COMMAND is set, the contents of the variable are executed each time a command is ran before returing the command prompt.

[ Reply to This | # ]
Set the title of the Terminal window with cd
Authored by: packy on Jun 23, '06 04:51:37AM

The command alluded to here is setting your prompt to include the xterm escape sequences that allow you to set the title of the current window. If you print "\033]0some text\007" to the screen, it won't show up as text on the screen, but, instead, the test "some text" will appear in the title of the window. Coupled with bash's escape sequences to provide information in the prompt, this can be a powerful tool.

(from the man page for bash)

<pre>PROMPTING
When executing interactively, bash displays the primary prompt PS1 when
it is ready to read a command, and the secondary prompt PS2 when it
needs more input to complete a command. Bash allows these prompt
strings to be customized by inserting a number of backslash-escaped
special characters that are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May
26")
\D{format}
the format is passed to strftime(3) and the result is
inserted into the prompt string; an empty format results
in a locale-specific time representation. The braces are
required
\e an ASCII escape character (033)
\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
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patchelvel (e.g., 2.00.0)
\w the current working directory
\W the basename of the current working directory
\! 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
\[ begin a sequence of non-printing characters, which could
be used to embed a terminal control sequence into the
prompt
\] end a sequence of non-printing characters

The command number and the history number are usually different: the
history number of a command is its position in the history list, which
may include commands restored from the history file (see HISTORY
below), while the command number is the position in the sequence of
commands executed during the current shell session. After the string
is decoded, it is expanded via parameter expansion, command substitu-
tion, arithmetic expansion, and quote removal, subject to the value of
the promptvars shell option (see the description of the shopt command
under SHELL BUILTIN COMMANDS below).</pre>

Personally, my .bashrc contains PS1="\[\033]0;\u @ \h: \w\007\]\\\$ ", which lets me keep track of where I am and what I'm logged in as.



[ Reply to This | # ]
How about the other way round?
Authored by: xth on Jul 12, '11 03:12:32AM

I am trying to do the opposite - READ the current window title. Anybody knows how to do that from within Bash?



[ Reply to This | # ]