Incorrect behavior of ANSI '\033[K' at the end of a line. #20336

Closed
opened 2026-01-31 07:10:41 +00:00 by claunia · 6 comments
Owner

Originally created by @latlaj on GitHub (Aug 7, 2023).

Windows Terminal version

1.17.11461.0

Windows build number

10.0.22621.0

Other Software

OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
grep (GNU grep) 3.0

Steps to reproduce

First ssh to linux.
Creat a file 'test.txt' like this:

abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef
grep 'abcd' test.txt --color=always

If Colored String at the end of the line, the last character will be lost. If Colored String at the begin of the line, the last character of the previous line will be lost.

grep 'abcd' test.txt --color=always > test.color

download the test.color file, it looks like:

\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef

It seems like \033[K clean the last character. How can I avoid it?

Expected Behavior

No response

Actual Behavior

char-lost

Originally created by @latlaj on GitHub (Aug 7, 2023). ### Windows Terminal version 1.17.11461.0 ### Windows build number 10.0.22621.0 ### Other Software OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3 grep (GNU grep) 3.0 ### Steps to reproduce First ssh to linux. Creat a file 'test.txt' like this: ```txt abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef ``` ```bash grep 'abcd' test.txt --color=always ``` If Colored String at the end of the line, the last character will be lost. If Colored String at the begin of the line, the last character of the previous line will be lost. ```bash grep 'abcd' test.txt --color=always > test.color ``` download the test.color file, it looks like: ```txt \033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef ``` It seems like \033[K clean the last character. How can I avoid it? ### Expected Behavior _No response_ ### Actual Behavior ![char-lost](https://github.com/microsoft/terminal/assets/39641430/48074be8-d484-4aa1-856d-9d1e904d879c)
claunia added the Issue-QuestionNeeds-TriageIssue-BugResolution-Answered labels 2026-01-31 07:10:42 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Aug 7, 2023):

what

@lhecker any ideas if is this the same thing you were also just seeing with ssh?

@zadjii-msft commented on GitHub (Aug 7, 2023): _what_ @lhecker any ideas if is this the same thing you were also just seeing with `ssh`?
Author
Owner

@lhecker commented on GitHub (Aug 7, 2023):

No, that one only happens with OpenConsole.

@lhecker commented on GitHub (Aug 7, 2023): No, that one only happens with OpenConsole.
Author
Owner

@j4james commented on GitHub (Aug 8, 2023):

There are two issue to be aware of here:

  1. When you write out a line of text that's exactly the width of the screen, the cursor doesn't immediately wrap onto the next line, but instead sets a flag and leaves the cursor in the last column. If more text is received while that flag is set, the cursor will move to the next line before adding the new content to the buffer. But if the terminal receives an EL command at that point, it's going to erase the character in the last column, because that's still where the cursor is positioned.

  2. There are a number of operations that are expected to reset that "end of line" flag, and EL is one of them. So after you've executed that EL command, any additional text you output at that point is expected to start in the last column rather than on the next line.

What we're seeing in the screen capture above is the effect of part 1, but not part 2. So that's why one of the characters is being blanked out rather than overwritten. We only implemented part 2 in PR #14936, which I don't think is included in version 1.17.

Either way you're going to end up with a missing character if you execute EL at the end of a line, but that's expected behavior. This is technically a bug in grep, because it really shouldn't be using EL like that. You can read more about it in the XTerm FAQ.

@j4james commented on GitHub (Aug 8, 2023): There are two issue to be aware of here: 1. When you write out a line of text that's exactly the width of the screen, the cursor doesn't immediately wrap onto the next line, but instead sets a flag and leaves the cursor in the last column. If more text is received while that flag is set, the cursor will move to the next line before adding the new content to the buffer. But if the terminal receives an `EL` command at that point, it's going to erase the character in the last column, because that's still where the cursor is positioned. 2. There are a number of operations that are expected to reset that "end of line" flag, and `EL` is one of them. So after you've executed that `EL` command, any additional text you output at that point is expected to start in the last column rather than on the next line. What we're seeing in the screen capture above is the effect of part 1, but not part 2. So that's why one of the characters is being blanked out rather than overwritten. We only implemented part 2 in PR #14936, which I don't think is included in version 1.17. Either way you're going to end up with a missing character if you execute `EL` at the end of a line, but that's expected behavior. This is technically a bug in grep, because it really shouldn't be using `EL` like that. You can read more about it in the [XTerm FAQ](https://invisible-island.net/xterm/xterm.faq.html#grep_colors).
Author
Owner

@j4james commented on GitHub (Aug 8, 2023):

I should also note that there was a workaround for this issue that was mentioned in the grep bug report. If you do something like this:

GREP_COLORS="ne" grep 'abcd' test.txt --color=always

Then grep won't inject those EL sequences everywhere, so you shouldn't lose any characters.

@j4james commented on GitHub (Aug 8, 2023): I should also note that there was a workaround for this issue that was mentioned in the grep bug report. If you do something like this: ``` GREP_COLORS="ne" grep 'abcd' test.txt --color=always ``` Then grep won't inject those `EL` sequences everywhere, so you shouldn't lose any characters.
Author
Owner

@latlaj commented on GitHub (Aug 8, 2023):

@j4james Thanks a lot for finding the answer for me.

@latlaj commented on GitHub (Aug 8, 2023): @j4james Thanks a lot for finding the answer for me.
Author
Owner

@zadjii-msft commented on GitHub (Aug 8, 2023):

Well, I think that answers that. Thanks for the detailed writeup!

@zadjii-msft commented on GitHub (Aug 8, 2023): Well, I think that answers that. Thanks for the detailed writeup!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20336