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

showing the current job in the window title



Hi,

I recently decided I wanted to show the current job I am using in my
xterm window titles.  I found, with some help, an example on this
mailing list but it was incomplete and not functional.  So I thought I
would share my success with you all, in case other people would be
interested to do the same in the future.

One interesting thing about it is that "builtin jobs %+" (or %1, %2,
etc) does not seem to work at all in preexec().  It would give off
strange errors instead such as "no such job: 1".  However using grep I
was able to attain the same result.

preexec () {
 local -a cmd
 local -a job

 if [ $#1 -gt 512 ];then 1=${1[1,512]}; fi
 cmd=(${(z)1})
 case $cmd[1] in
   (fg)
     if (( $#cmd == 1 )); then
       builtin jobs | grep "^\[[0-9]\+\]\W\++" | read job
       job=$job[19,-1]
     else
       builtin jobs | grep "^\[${(Q)cmd[2][2,-1]}\]" | read job
       job=$job[19,-1]
     fi ;;
   (r)
     builtin history -n -1 | read job ;;
   (%*)
     builtin jobs | grep "^\[${(Q)cmd[1][2,-1]}\]" | read job
     job=$job[19,-1] ;;
   (exec)
     job=($cmd)
     shift job ;;
   (*)
     job=$cmd ;;
 esac

 ct -e "`print -P "%n@%m"` {$job} [`print -P "%100<...<%~%<<"`]"
}

ct () {
	local MATCH
	if [[ "$1" = "-e" ]]
	then
		print -n "\033]0;${(q)${2//
/\\n}//(#m)[-]/$MATCH}\007"
	elif [[ "$1" = "-t" ]]
	then
		pts="$2"
		ct -e "$3" > /dev/pts/$pts
	else
		ct -e "$*"
	fi
}

Yay for zsh!



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