Colored grep output getting fragmented by watch command

colorsgrepwatch

I'm trying to run watch, so that it shows me the output from my grep command, while I am running over files and changing things.

The usual way of doing this works fine, but lacks colors:

watch grep 'something' **/*

…and since I do want colors, I did this:

watch grep 'something' **/* --color=always

It's still ugly, because watch doesn't parse the escape codes that make the colors work. So then I found this:

watch --color grep 'something' **/* --color=always

And well… it doesn't work. The output is fragmented / mixed up in some weird way. It shows me only a single line for some reason, while without watch --color the grep outputs about 20.

How can I make watch work with colorized output from grep?


Isolated case:

Using the following:

mkdir grep_test; cd grep_test
echo "asdsad\nasdasd\nsaasdasd" > file1 
echo "test\n123\ntest" > file2

I create two files, and run the following command:

grep test * --color=always

Which works as expected, and outputs (with colors and whatnot):

file2:test
file2:test

But when I use watch:

watch --color 'grep test * --color=always'

It outputs nothing. Without the --color option it shows the right output, though without the ANSI escapes interpreted.
I'm running this on Fedora 25, in tmux session with zsh inside.

Also, watch -v output is:

watch from procps-ng 3.3.10

Best Answer

It turns out that watch does work with color output. But specifically for grep you should make some workaround (according to grep --color adds ANSI code ESC[K - This can change displayed text)

watch --color "GREP_COLORS=ne grep --color=always .* *"
Related Question