I encountered the 'color-stealing' issues with watch, it's because watch uses a simple /bin/sh output style, which clobbers any color, aliases, shortcuts... the whole shebang!
So I cobbled this together, placed in my .bashrc, allows me to use all my own aliases, shortcuts and whatnot:
Usage:
watcher 20 'somecommand | apipe | grep'
The number is the delay between updates, in seconds, the command can include anything you can quote.
###### 'watch' workalike, lets me use my aliases and such
function watcher() { WATCHERTIME=$1 ; WATCHERFILE=/tmp/watcher$$ ; shift ; while true ; do WATCHERHEIGHT=$(($LINES - 5)) ; ( eval $* ) | tail -n ${WATCHERHEIGHT} > ${WATCHERFILE} 2>/dev/null; clear ; /bin/echo -n "Every ${WATCHERTIME} seconds - " ; date ; /bin/echo ; cat ${WATCHERFILE} ; \rm -f ${WATCHERFILE} ; /bin/echo ; /bin/echo "==" ; sleep ${WATCHERTIME} ; done ; }
To break it down:
function watcher()
{
WATCHERTIME=$1
WATCHERFILE=/tmp/watcher$$
shift
while true; do
WATCHERHEIGHT=$(($LINES - 5))
( eval $* ) | tail -n ${WATCHERHEIGHT} > ${WATCHERFILE} 2>/dev/null
clear
/bin/echo -n "Every ${WATCHERTIME} seconds - "
date
/bin/echo
cat ${WATCHERFILE}
\rm -f ${WATCHERFILE}
/bin/echo
/bin/echo "=="
sleep ${WATCHERTIME}
done
}
It determines the current screen height in lines, subtracts out enough for its own output, then repeatedly executes the command given, then clears the screen, displays the output and waits for the next loop. It displays a short '==' at the bottom to signify that's where the output ended. Sometime's it's helpful to know this.
I did it this way so there was smallest possible delay when displaying. if you don't capture output and then display, you get a long pause, then the output... icky.
Since it doesn't clobber colors, you get everything you're used to getting. Enjoy!
Best Answer
When
git status
is run underwatch
, it is able to detect that its standard output is not a terminal, meaning it will not output colors if thecolor.status
setting is set toauto
. To forcegit status
to always output colors (even underwatch
), setcolor.stats
toalways
, e.g.to set the setting permanently, or as @ChrisJonsen points out, use
git -c color.status=always status
to rungit status
with a one-time override.