photo
Jordan Sissel
geek

Fri, 08 Aug 2003

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!

Comments: 0 (view comments)
Tags:
Permalink: /geekery/45
posted at: 22:42

Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '08 Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials SSH Tunneling (Video)

Tag Cloud

Calendar

< August 2003 >
SuMoTuWeThFrSa
      1 2
3 4 5 6 7 8 9
10111213141516
17181920212223
24252627282930
31      

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati