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


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: 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 | # ]