Next Previous Contents

5. Xterm Title Bar Manipulations

Non-printing escape sequences can be used to produce interesting effects in prompts. To use these escape sequences, you need to enclose them in \[ and \] (as discussed in Non-Printing Characters in Prompts), telling Bash to ignore this material while calculating the size of the prompt. Failing to include these delimiters results in line editing code placing the cursor incorrectly because it doesn't know the actual size of the prompt. Escape sequences must also be preceded by \033[ in Bash prior to version 2, or by either \033[ or \e[ in later versions.

If you try to change the title bar of your Xterm with your prompt when you're at the console, you'll produce garbage in your prompt. To avoid this, test the TERM environment variable to tell if your prompt is going to be in an Xterm.


function proml
{
case $TERM in
    xterm*)
        local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        local TITLEBAR=''
        ;;
esac

PS1="${TITLEBAR}\
[\$(date +%H%M)]\
[\u@\h:\w]\
\$ "
PS2='> '
PS4='+ '
}

This is a function that can be incorporated into ~/.bashrc. The function name could then be called to execute the function. The function, like the PS1 string, is stored in the environment. Once the PS1 string is set by the function, you can remove the function from the environment with unset proml. Since the prompt can't change from being in an Xterm to being at the console, the TERM variable isn't tested every time the prompt is generated. I used continuation markers (backslashes) in the definition of the prompt, to allow it to be continued on multiple lines. This improves readability, making it easier to modify and debug.

I define this as a function because this is how the Bashprompt package (discussed later in this document: The Bash Prompt Package) deals with prompts: it's not the only way to do it, but it works well. As the prompts you use become more complex, it becomes more and more cumbersome to type them in at the prompt, and more practical to make them into some sort of text file. In this case, to test this at the prompt, save the above as a text file called "proml". You can work with it as follows:

[giles@nikola:/bin (4.498 Mb)]$ cd          -> Go where you want to save the prompt
[giles@nikola:~ (0 Mb)]$ vi proml           -> Edit the prompt file
...                                         -> Enter the text given above
[giles@nikola:~ (0 Mb)]$ source proml       -> Read the prompt function
[giles@nikola:~ (0 Mb)]$ proml              -> Execute the prompt function

The first step in creating this prompt is to test if the shell we're starting is an xterm or not: if it is, the shell variable (${TITLEBAR}) is defined. It consists of the appropriate escape sequences, and \u@\h:\w, which puts <user>@<machine>:<working directory> in the Xterm title bar. This is particularly useful with minimized Xterms, making them more rapidly identifiable. The other material in this prompt should be familiar from previous prompts we've created.

The only drawback to manipulating the Xterm title bar like this occurs when you log into a system on which you haven't set up the title bar hack: the Xterm will continue to show the information from the previous system that had the title bar hack in place.


Next Previous Contents