After some searching for ^[[K
escape sequence, reading half of a book about VT100 terminal and checking man grep
I have found that setting environment variable GREP_COLORS
to
GREP_COLORS=ne
Gives desired output:
$ export GREP_COLORS=ne
$ grep -E --color=always 'jumps^M?$' dos.txt unix.txt
dos.txt:The quick brown fox jumps
unix.txt:The quick brown fox jumps
$ grep -E --color=always 'jumps^M?$' dos.txt unix.txt | cat -v
^[[35mdos.txt^[[m^[[36m:^[[mThe ... ^[[01;31mjumps^M^[[m
^[[35munix.txt^[[m^[[36m:^[[mThe ... ^[[01;31mjumps^[[m
From grep
man page:
ne Boolean value that prevents clearing to the
end of line using Erase in Line (EL) to Right
(\33[K) each time a colorized item ends.
This is needed on terminals on which EL is
not supported. It is otherwise useful on
terminals for which the back_color_erase
(bce) boolean terminfo capability does not
apply, when the chosen highlight colors do
not affect the background, or when EL is too
slow or causes too much flicker. The default
is false (i.e., the capability is omitted).
In my case it works good even if I set highlight color to something that change background:
export GREP_COLORS="ne:mt=41;38"
Now the interesting question is why ^[[K
produces this blank line.
Character ^M
means carriage return without going to next line:
$ echo -e "start^Mend"
endrt
^[[K
clears line from cursor to right and then writes rest of line:
$ echo -e "start\033[Kend"
startend
However when you put ^M
before ^[[K
it removes content:
$ echo -e "start^M\033[Kend"
end
After writing start cursor goes to the beginning of line then ^[[K
removes everything and rest of the line is written. In case of grep
output first line writes everything up to word jumps, then goes back to beginning of line ^M
, writes harmless ^[[m
sequence and ^J
that goes to new line. This is why ^[[K
after ^M
clears whole line.
^M?
does not work for me. Is there something special about it? – Oskar Skog Mar 09 '17 at 19:40echo -en "^[[01;31m^[[Kjumps\r^[[m^[[K\r\n"
. – Thor Mar 09 '17 at 20:49echo
example above? It exhibits the same behavior. – Thor Mar 09 '17 at 21:29