Translate

Archives

Dynamically Updating Xterm Title using Ksh93

While it is easy to dynamically set your shell prompt and the title of your terminal window if you are using the bash shell, it is far more difficult to do so in the Korn Shell.

Typically, as in Rich Lister’s How to change the title of an xterm, the offered solution is something like the following:

HOST=`hostname`
HOST=${HOST%%.*}
PS1='^[]0;${USER}@${HOST}: ${PWD##${HOME}/}^Gksh$ '


The problem with such a solution is that it sets the prompt and the xterm window title only once.

If you wish to dynamically set the xterm title and the shell prompt, you must use a Korn shell get discipline function on the PS1 variable. Here is an example of such a discipline function:

# set ksh prompt and xterm title
_PSX='$( p="${PWD/~(El)${HOME}/\~}"
         printf "%s@%s:%s" "${LOGNAME}" "$(hostname -s)" "${p}"
)'

function PS1.get
{
   .sh.value="^[]0;${_PSX}^G${_PSX}$ "
}


Disciple functions have a number of defined internal compound variables, all starting with .sh. See the ksh93 manpage for more information about disciple functions and compound variables. The code to change the xterm window title is ^[]0;${_PSX}^G. Instead of the embedded escape and bell, you could use octal values.

Here are two screenshots of the above code in action after being saved to ~/.kshrc:

Note – this solution only works for ksh93. It does not work for ksh88, pdksh or mirsh.

In a future post, I will show you how to have bash-like custom prompts.

Enjoy!

1 comment to Dynamically Updating Xterm Title using Ksh93

  • Jean-Philippe Henry

    Hello,

    If I may leave a suggestion, I would define:

    typeset start_title=$’\033]0′ stop_title=$’\a’

    and I would use:

    .sh.value=”$start_title;${_PSX}$stop_title${_PSX}$ ”

    Thanks for all your nice and valuable works.

    Cheers

    Jean-Philippe