Search this site

Metadata

Articles

Projects

Presentations

I am the winner, take THAT ksh!

I've been trying off-and-on for some time now to get dynamically updating xterm titles in ksh. Sure, you could set your prompt to something like:

XTERM="^[]2;Yay for titles^G"
PS1=$XTERM'$HOST($PWD) >'

This works all good and dandy until you try to write something on the command line longer than what's displayable on a single row of text, you'll see that ksh thinks the end of your line is shorter than where it's at. This is due to the problem that your xterm title is still physically a part of your prompt. This was too annoying for me to bother dealing with since my prompt at a minimum is something around 20 characters.
So I gave up trying different ways, and today I had another idea. $() and `` doesn't return anything printed to stderr. We'll use that to our advantage!

PROMPT='$HOST($(/bin/pwd | sed -Ee "s,(/usr)?$HOME,~,")) [!] '$EXITCODE'$Z '
EXITCODE='$(A=$? && [ $A -ne 0 ] && echo "!!${A}!! ")'
XTERM= 
case $TERM in
  aterm|xterm|dtterm)
    XTERM='$(echo -n "\033]2;'$PROMPT'\007" 1>&2)'
    ;;
esac
export PS1=$XTERM$PROMPT

As you can see, the $(echo ...) has stdout redirected to stderr, and therefore our xterm title text gets completely ignored by PS1 when it's executed.
Huzzah!

0 responses to 'I am the winner, take THAT ksh!'

Showing last 0 comments... (Click here to view all comments)


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: