0

I am on Windows 10 and I am unable to display ANSI Escape Sequences coloring.

I've already set VirtualTerminalLevel to 1 in registry under HKEY_CURRENT_USER/Console

Registry Entry:

Registry Entry

As suggested here: https://superuser.com/a/1300251/1706673

When I open up cmd.exe, I type this:

> echo ^<ESC^>[7m [7mInverse[0m
<ESC>[7m [7mInverse[0m

It just prints it out as a regular text, instead of the ANSI colored text. I've also done the customary rebooting ritual just in case, and nothing changed.

Caveat: I don't wish to use third party tools, windows powershell, or windows terminal. I just want to use cmd.exe that came with the system. From this thread, it states that since Windows v1511 (build 10586, from 2015), the system should be able to display ANSI Escape Sequences anyway, so I just want to get this working in my command prompt.

What's wrong with my system?

Jackdaw
  • 1,087
  • 2
  • 6
  • 19
doejoe
  • 1
  • 1

2 Answers2

4

Those aren't complete escape sequences; they're missing the actual ESC character (byte 0x1B) in front of the [7m. In other languages you see it written down as \e or \033 or \x1B, but Cmd has no equivalent syntax – so the script you found actually includes the ESC literally. But because it's a "control" character, often it gets lost soon as the script is copied into a website.

Press Ctrl[ to insert this character at the Cmd prompt (you will see ^[):

C:\> echo test Ctrl[[1mthis is boldCtrl[[m
test this is bold

(Note that the formatting codes have a second, normal [ character – you need to include both.)

One way to include this in your file is to have it there literally, e.g. create an %ESC% variable:

C:\> echo set "ESC=Ctrl[">>test.bat
u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 1
    Just to note: doesn't work on every keyboard layout. With my German settings, to get `[`, I have to `[AltGr]8`. `[Ctrl][AltGr]8` doesn't work (because `[AltGr]` is the same as `[Ctrl][ALT]`). (Temporarily switching to US keyboard layout does the trick though - if you know where `[` is located on a US keyboard) – Stephan Jul 02 '22 at 13:09
3

You have to use chr(27) (a.k.a "ESC") to make the ansi sequence complete.
You can obtain it with:

for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set ESC=%%b

(batch file syntax; if you want to do it directly on command line, use a single % instead of %%)

Then you can use it like:

echo ^<ESC^>[7m %ESC%[7mInverse%ESC%[0m
Stephan
  • 1,548
  • 1
  • 10
  • 10