How to make win32 console recognize ANSI/VT100 escape sequences?

86,361

Solution 1

[UPDATE] For latest Windows 10 please read useful contribution by @brainslugs83, just below in the comments to this answer.

While for versions before Windows 10 Anniversary Update:

ANSI.SYS has a restriction that it can run only in the context of the MS-DOS sub-system under Windows 95-Vista.

Microsoft KB101875 explains how to enable ANSI.SYS in a command window, but it does not apply to Windows NT. According to the article: we all love colors, modern versions of Windows do not have this nice ANSI support.

Instead, Microsoft created a lot of functions, but this is far from your need to operate ANSI/VT100 escape sequence.

For a more detailed explanation, see the Wikipedia article:

ANSI.SYS also works in NT-derived systems for 16-bit legacy programs executing under the NTVDM.

The Win32 console does not natively support ANSI escape sequences at all. Software such as Ansicon can however act as a wrapper around the standard Win32 console and add support for ANSI escape sequences.

So I think ANSICON by Jason Hood is your solution. It is written in C, supports 32-bit and 64-bit versions of Windows, and the source is available.

Also I found some other similar question or post which ultimately have been answered to use ANSICON:

Solution 2

Starting from Windows 10 TH2 (v1511), conhost.exe and cmd.exe support ANSI and VT100 Escape Sequences out of the box (although they have to be enabled).

See my answer over at superuser for more details.

Solution 3

Base on @BrainSlugs83 you can activate on the current Windows 10 version via register, with this command line:

REG ADD HKCU\CONSOLE /f /v VirtualTerminalLevel /t REG_DWORD /d 1

Solution 4

Starting from Windows 10, you can use ENABLE_VIRTUAL_TERMINAL_PROCESSING to enable ANSI escape sequences:

https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx

Solution 5

For Python 2.7 the following script works for me fine with Windows 10 (v1607)

import os

print '\033[35m'+'color-test'+'\033[39m'+" test end"
os.system('') #enable VT100 Escape Sequence for WINDOWS 10 Ver. 1607
print '\033[35m'+'color-test'+'\033[39m'+" test end"

Result should be:

[35mcolor-test[39m test end

color-test test end
Share:
86,361

Related videos on Youtube

Witiko
Author by

Witiko

Updated on July 05, 2022

Comments

  • Witiko
    Witiko almost 2 years

    I'm building a lightweight version of the ncurses library. So far, it works pretty well with VT100-compatible terminals, but win32 console fails to recognise the \033 code as the beginning of an escape sequence:

    # include <stdio.h>
    # include "term.h"
    
    int main(void) {
      puts(BOLD COLOR(FG, RED) "Bold text" NOT_BOLD " is cool!" CLEAR);
      return 0;
    }
    

    Screenshot

    What needs to be done on the C code level, in order that the ANSI.SYS driver is loaded and the ANSI/VT100 escape sequences recognized?

  • Witiko
    Witiko about 11 years
    That's quite an unwieldy solution, not to mention the fact that this only allows for the coloring of a text - not the rest of ANSI.SYS codes. users.cybercity.dk/~bse26236/batutil/help/ANSI~S_S.HTM
  • est
    est over 8 years
    I once did a hack to make ANSI work in Windows XP without any thirdparty tools groups.google.com/forum/#!topic/alt.msdos.batch.nt/YZnoq80Mc‌​ds
  • Anatol Belski
    Anatol Belski almost 8 years
    The 105xx builds was the only builds having VT100 sequences enabled by default. In previous and later builds it's disabled by default cause it was enabled by mistake. See here for the explanation wpdev.uservoice.com/forums/…
  • Rich Turner
    Rich Turner almost 7 years
    How times change ;) In Windows 10 Anniversary Update and beyond, Windows Console was updated with pretty solid support for ANSI/VT sequences, and in Creators Update, was updated to support 24-bit RGB colors too: blogs.msdn.microsoft.com/commandline/2016/09/22/…
  • Franco Rondini
    Franco Rondini almost 7 years
    Answer updated according to Rich Turner contribution
  • Franco Rondini
    Franco Rondini over 6 years
    @Simon thanks, I removed the link because it doesn't work btw there is an archived version of kb/101875 here
  • Admin
    Admin about 6 years
    That starts to look like black magic and no mention of how to turn off the console setting afterwards? curiously, it works for me, but unclear why from the magical empty string.
  • BrainSlugs83
    BrainSlugs83 over 5 years
    FYI, in latest Windows 10, you can enable ANSI in conhost via the following reghack -- in HKCU\Console create a DWORD named VirtualTerminalLevel and set it to 0x1; then restart cmd.exe. -- You can test it with the following powershell "?[1;31mele ?[32mct ?[33mroni ?[35mX ?[36mtar ?[m".Replace('?', [char]27);.
  • BrainSlugs83
    BrainSlugs83 over 5 years
    FYI, in latest Windows 10, you can enable ANSI in conhost via the following reghack -- in HKCU\Console create a DWORD named VirtualTerminalLevel and set it to 0x1; then restart cmd.exe. -- You can test it with the following powershell "?[1;31mele ?[32mct ?[33mroni ?[35mX ?[36mtar ?[m".Replace('?', [char]27);.
  • Franco Rondini
    Franco Rondini over 5 years
    Thanks @brainslugs83 - Answer updated to cite your comment
  • Ken Ingram
    Ken Ingram over 5 years
    I tried this and then docker-machine refused to connect.
  • whatisit
    whatisit over 5 years
    If you wrap the printed material in parentheses (...) this works in Python 3 on Windows 10 (v1703). (i.e. print('\033[35m'+'color-test'+'\033[39m'+" test end"))
  • Boop
    Boop over 5 years
    Does someone have a source or know what os.system('') changes to the console ?
  • Arung Isyadi
    Arung Isyadi almost 5 years
    I'm sorry for so long but the method above was never tested for docker.
  • Youssef13
    Youssef13 over 4 years
    Is this supported in all Windows 10 versions ? Or available from a specific update ?
  • Daniel De León
    Daniel De León over 4 years
    in the firsts version of Windows 10 is not supported, but in the currents one work fine.
  • Youssef13
    Youssef13 over 4 years
    I want to know the specific version that supported this.
  • Richard Goulter
    Richard Goulter over 4 years
    os.system('') works apparently because of a bug in cmd.exe (cmd.exe enables VT mode, but doesn't disable it on exit). For more info, and better code to enable VT mode, see discussion on issue 30075 on python.org
  • InQβ
    InQβ almost 3 years
    @Youssef13 According to superuser.com/a/1300251/702169, first supporting version is build 16257.
  • Andry
    Andry over 2 years
    Just for instance, you can recompile the app by using the ParseAndPrintString function directly.