Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Retrieving the terminal title / setting it using the name of the foreground process

On Tue, 25 Nov 2008 10:40:32 -0800 (PST)
Enny Ammis <enny_ammis@xxxxxxxxx> wrote:
> i'm fairly new to zsh (it's been 4 days now), and i've been spending
> some time customizing it. one thing i haven't been able to do yet is
> change a terminal's title so that it contains the name of the currently
> active process, i.e. "zsh" if i'm simply using the shell, "perl args" if
> i'm running a perl program, "emacs file.txt" if i'm running emacs

The simplest thing you can do is

  preexec() { print -nr $'\033'"]2;$1"$'\a' }

This is designed to avoid problems with the command line to be executed,
passed in $1, being interpreted specially, which is why it's more
complicated than you might first expect.

> and "zsh" again if i hit ^Z and send emacs to the background. 

Well, you can't trap the ^Z itself in an interactive shell.  However,
if you're just trying to get something to happen when you get back to
the prompt, it's easy.  If all you want is to cancel the stuff in the
title bar and replace it with zsh when a programme isn't running in the

  precmd() { print -nr $'\033'"]2;zsh"$'\a' }

I'm not sure if that was all there was too it.  You can get the information
about jobs from parameters; in a recent shell, the variable $jobstates,
contained in the zsh/parameter module, is the easiest to manipulate.
See the zshmodules manual.

If you have other information to go into the title you will need to
store that in a shell variable whereever it's generated and add it to
the information that's printed out.

  # wherever
  foo="... some stuff ..."

  # then...
  preexec() { print -nr $'\033'"]2;$foo$1"$'\a' }
  precmd() { print -nr $'\033'"]2;${foo}zsh"$'\a' }

If you want to do something different, short, specific questions are
most likely to get a useful answer.

Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/

Messages sorted by: Reverse Date, Date, Thread, Author