Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh prompt - checking if there are any background jobs

I'm customizing my zsh prompt and have found the following to check if there are any background jobs:

if [[ $(jobs | wc -l) -gt 0 ]]; then # has background job(s)
    number_jobs='J:${cyan}%j${no_color}'
else # no background job(s)
    number_jobs=""
fi

The problem I'm facing is that the code appears to be evaluated only when I open a new session rather than after each command making this rather useless. How can I have number_jobs re-evaluated after each command?

like image 794
Chauncey Garrett Avatar asked Apr 17 '12 15:04

Chauncey Garrett


1 Answers

zsh has a special prompt sequence, like C ternary operator. The below construction means if there are one or more jobs then print their number or print nothing:

 %(1j.%j.)
like image 152
yazu Avatar answered Dec 16 '22 21:12

yazu