WSL: ANSI colors no longer render in cmd.exe

Please use the following bug reporting template to help produce actionable and reproducible issues. Please try to ensure that the reproduction is minimal so that the team can go through more bugs!

https://github.com/symfony/symfony/issues/19520 confirms that other people are seeing the same problem.

  • Expected results ANSI colors should render in cmd.exe when applications output to stdout/stderr.
  • Actual results (with terminal output if applicable) Raw ANSI codes are seen. No colors are rendered.
  • Your Windows build number 10.0.14393
  • Steps / All commands required to reproduce the error from a brand new installation
  • Run this Java testcase:
public class Test {

    public static void main(String[] args) {
        System.out.println("\033[37;1mwhite\033[0m");
    }
}
  1. You will get this output: white
  2. Redirect the application stdout to foo.log and run type foo.log
  3. Notice that the output now reads white using white text

This is reproducible 100% of the time.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 39 (7 by maintainers)

Commits related to this issue

Most upvoted comments

As noted here and here, the ANSI VT can be enabled for SetConsoleMode-oblivious apps, without having to re-compile them, by changing the following registry key.

[HKEY_CURRENT_USER\Console]
"VirtualTerminalLevel"=dword:00000001

This changes the system-wide default for ANSI VT processing from opt-in to opt-out, so it will affect any/all apps that don’t express a preference. Works for Windows 10.16257 (and later) (and possibly earlier).

Given the very helpful information in a comment on this page by @zadjii-msft, this registry setting would appear to be the only workaround for apps that can’t be re-compiled to include a call to SetConsoleMode. Because yes, cmd.exe does respect this setting while it does the fiddling described in that remark.

HKEY_CURRENT_USERonsoleVirtualTerminalLevel -- Registry setting to enable ANSI sequence processing on Windows 10.16257 and later

Oh my, you leave the office for one weekend, and this happens 😛

There’s a lot going on in here, so let me try to clarify: The windows console only supports VT sequences of ANY kind with the ConsoleMode set to enable Virtual Terminal support. See This documentation on SetConsoleMode. Specifically, you need to turn on the output mode flag ENABLE_VIRTUAL_TERMINAL_PROCESSING.

By default, cmd.exe enables VT mode for itself. Now here’s the confusion:

  • In 10.0.10586 (or 1511 or TH2), it left this mode on when calling child applications. This meant that anything you launched from cmd would get the new behavior for free. Of course, we found that unsurprisingly broke compatibility, so we had to change that. So…
  • In 10.0.14393 (Anniversary update, or greater), cmd uses VT mode, but then reverts it for launching child processes. So if you wrote a console app that uses VT sequences after October 2015, then tried running it on an AU system, you’ll find that the sequences don’t work anymore. You’ll have to manually turn on VT support with SetConsoleMode yourself. IMPORTANT cmd also wipes out the changes a child makes to the mode. So you can’t call enable_vt.exe & myapp.exe and have the VT mode from enable_vt.exe pollute the mode of myapp.exe

To further confuse, bash.exe turns on VT mode itself, because obviously, it needs that.

I can’t really speak about the Java issue at hand, it’s been far too long since I’ve used Java, and I don’t really have any experience calling Win32 API’s from java. I did end up writing a snippet of code that enables VT support on Windows in python, so it can be done.

In conclusion: I’m going to leave this issue closed, as this is the proper, expected behavior. Confusing, but correct.

Also, in terms of hierarchy, @bitcrazed is the PM for the console, while I’m one of the devs

Just a note to anybody using Python.

You can enable ANSI using this:

import ctypes

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)

Use this to enable terminal colors by updating the registry using Java :

public class ENABLE_TERMINAL_COLORS_JAVA {
  public static void main(String[] args)
  {
    try { // TO ENABLE
      Process process = Runtime.getRuntime().exec("reg add HKEY_CURRENT_USER\\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f");
    }
    // TO DISABLE (Put it back to its original value) --> try {Process process = Runtime.getRuntime().exec("reg add HKEY_CURRENT_USER\\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000000 /f");}
    catch (Exception e) {
      e.printStackTrace(); // Catch and display exception
    }
  }
} 

Click to follow - GitHub Followers

@cowwoc For reporting bugs, you really need to be on the fast ring insider builds. The development here is done way faster than the Windows stable builds can keep up.

@zadjii-msft Thanks for the clarification. It helps a lot.

Out of curiosity, can you please provide concrete examples where enabling this flag by default broke app compatibility?

@fpqc I don’t really care about printf. I was just using it because you were doing the same in your testcase. The only thing I care about is getting this to work with Java.

  1. I tried the printf version you mentioned and got the same output (no colors).
  2. Can you please run my testcase on your end?
  3. Why isn’t ANSI support enabled by default (without having to enable ENABLE_VIRTUAL_TERMINAL_INPUT?
  4. Why were ANSI colors working for me (and others) in past releases and now they no longer do? This seems like a clear regression.
  5. How do I find out what console mode a process is running with? I want to reverse-engineer what Java is using.

ENABLE_VIRTUAL_TERMINAL_PROCESSING Is only available on Windows 10 and up, in the v2 console.

The v1 console (the only version of the console available on pre-Windows 10) probably doesn’t ignore flags that it doesn’t support, so you could probably SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING and then have it roundtrip to your next call to GetConsoleMode, but that doesn’t imply that it’s actually enabled. I know that the v2 console will ignore flags it doesn’t understand, but I’m not positive about the v1.

Edit: To add, VirtualTerminalLevel will additionally do nothing on the v1 console.

@WASasquatch The error you pasted has nothing to do with Windows. Please google NoClassDefFoundError and discuss with the jansi authors if necessary.

@fpqc There is no way to set console modes in Java without using JNI (which is not a realistic option for my use-case). It is also unlikely Sun/Oracle would enable this in the near future.

I think it would help if someone from Microsoft address point 4: why were ANSI colors working in 10.0.10586 but got broken in version 10.0.14393? Something must have changed and I’m not convinced yet this was intentional.