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

Re: can i have jobs in my prompt?

zefram wrote:
> Matthias Kopfermann wrote:
> >i forget how many jobs are running, especially if they are in the background.
> >what would you do here?
> function precmd {
>   if jobs % >& /dev/null; then
>     psvar=("*")
>   else
>     psvar=("")
>   fi
> }

Here is something I whipped up many years ago.  It would only list
stopped jobs, which is what I wanted, because if a job was running I
probably already know about it.  This goes inside my precmd function.  A
few things to note:

- This lists jobs like this:
  j[1+ 3- 4]
  where job 1 is the current job and 3 is the last job and 4 is some
  other job, just like 'jobs -l' prints.
- For running jobs change the -s to -r in the first line.
- this creates lots of temporary files in /tmp.  If you want them to be
  cleared up afterwards don't forget to have a trap on EXIT on your
  interactive shell.
- older versions of zsh would print jobs to stderr, not stdout.  Alter
  the first line appropriately.
- gawk works in place of nawk if you don't have the latter.
- somewhere in my $PROMPT there is a %v that uses the value I put into

This mailing list flourishes on ideas.  I am always looking for new
ideas of things to put in my prompt.  Presently I list jobs, pwd,
folders with new mail, the status of a shat program used here at Monash,
and shell level.  Anyone else got neat shell prompt stuff?

Debbie Pickett  http://www.cs.monash.edu.au/~debbiep/  tlm@xxxxxxxxxxxxxxxxxxxxx
                  "Welcome to the food chain." - _FernGully_

# Get a list of suspended jobs to put in the command line.
# Have to use a temporary file because it's the only way
# we can run the jobs builtin and set a variable within the current
# shell - doing either of these in a subshell defeats the purpose.
# The jobs command outputs to stderr.  Use the current hostname
# and process ID to ensure that there's no problems even if
# /tmp is mounted across filesystems.
builtin jobs -s >! /tmp/jobs$HOST$$
if [[ -s /tmp/jobs$HOST$$ ]]
  # There's at least one suspended job.  Glean the job numbers from
  # the file.  Surround the text with the relevant description and
  # proper number of spaces.
  psvar[4]="j`nawk \
    '/^\[/ { 
      if (match (\$1, /[0-9]+/)) {
        jobs = jobs \" \" substr (\$1, RSTART, RLENGTH)
      if (match (\$2, /^\+|-$/)) {
        jobs = jobs \$2
    END {
      if (jobs != \"\") {
        print \"[\" substr (jobs, 2) \"]\"
    }' < /tmp/jobs$HOST$$` $XTTITLEBETWEEN"
  # No jobs - we can skip the call to nawk.

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