Hi, > Where the `[ K` come from? [...] What does `[ K` mean?
It's about terminal emulation and the grep tool, offtopic to coreutils, but let me respond with some details. The traditional behavior of hardware terminals (VTxxx), and thus xterm which aims to fully emulate their behavior, is: Whenever the bottom right corner of the terminal is reached, and printing a letter causes a wrap, thus the contents move upwards and a new line appears at the bottom, this entire new line receives the currently active background color. So, if you configure "grep" to highlight something with a nondefault background (let's say yellow), and the matching segment happens to span across two lines, and this happens when you've already reached the bottom of your terminal, the entire bottom line becomes yellow. This looks unexpected and unpleasant. As a workaround, grep by default emits `^[ [ K` after resetting to the default color at the end of the matching segment. This sequence erases to the end of the line using the current background color, which in this case is the default color. Unfortunately this workaround introduces an even more severe bug. `^[ [ K` has the side effect that when the cursor is in the last column and the "pending wrap" bit is set (that is: a letter has already been written to the last column, the next letter will autowrap to the beginning of the new line and be printed there), this sequence clears the "pending wrap" bit before erasing to the end of the line. That is, if the matching segment happens to end just at the right margin, the said sequence removes its last letter. It's unclear to me why "grep" by default choses to go with the more serious issue rather than the cosmetic one. Maybe its developers just didn't fully understand the story, or the bug fell through the cracks and they should be pinged. See the grep issue with workaround at [1]. I have vague memories that a loooong time ago I asked coreutils (er, probably fileutils) "ls" to do the same trick, maybe it even got implemented and released too, but then it got reverted presumably because this more severe issue was discovered. Or maybe I remember incorrectly and it was only me patching my own ls. Some terminal emulators intentionally differ here from xterm, e.g. VTE (gnome-terminal and others) differs in two ways: `^[ [ K` doesn't clear the "pending wrap" flag, and the newly appearing line (if it appears due to autowrapping) doesn't receive the current background color. As a result, grep behaves "as expected" no matter if you let it emit `^[ [ K` or disable it. If you're interested in further details and discussions behind these changes for VTE, you'll find them at [2] and [3]. cheers, egmont [1] http://savannah.gnu.org/bugs/?36831 [2] https://bugzilla.gnome.org/show_bug.cgi?id=740789 [3] https://bugzilla.gnome.org/show_bug.cgi?id=754596
