How to make win32 console recognize ANSI/VT100 escape sequences?
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:
- How to load ANSI escape codes or get coloured file listing in WinXP cmd shell?
- how to use ansi.sys in windows 7
- How can I get cmd.exe to display ANSI color escape sequences?
- ansi color in windows shells
- enable ansi colors in windows command prompt
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
Related videos on Youtube
Witiko
Updated on July 05, 2022Comments
-
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; }
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?
-
jfs almost 7 years
-
Andreas Rejbrand almost 6 yearsPlease notice that the rules of the game have changed drastically as of recent versions of Windows 10.
-
BrainSlugs83 over 5 yearsFYI, in latest Windows 10, you can enable ANSI in conhost via the following reghack -- in
HKCU\Console
create aDWORD
namedVirtualTerminalLevel
and set it to0x1
; then restart cmd.exe. -- You can test it with the following powershell"?[1;31mele ?[32mct ?[33mroni ?[35mX ?[36mtar ?[m".Replace('?', [char]27);
. -
Bogdan Mart over 5 yearsupd docs.microsoft.com/en-us/windows/console/… here how it could be enabled without editing registry, and possibly breaking other applications
-
-
Witiko about 11 yearsThat'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 over 8 yearsI once did a hack to make ANSI work in Windows XP without any thirdparty tools groups.google.com/forum/#!topic/alt.msdos.batch.nt/YZnoq80Mcds
-
Anatol Belski almost 8 yearsThe 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 almost 7 yearsHow 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 almost 7 yearsAnswer updated according to Rich Turner contribution
-
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 about 6 yearsThat 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 over 5 yearsFYI, in latest Windows 10, you can enable ANSI in conhost via the following reghack -- in
HKCU\Console
create aDWORD
namedVirtualTerminalLevel
and set it to0x1
; then restart cmd.exe. -- You can test it with the following powershell"?[1;31mele ?[32mct ?[33mroni ?[35mX ?[36mtar ?[m".Replace('?', [char]27);
. -
BrainSlugs83 over 5 yearsFYI, in latest Windows 10, you can enable ANSI in conhost via the following reghack -- in
HKCU\Console
create aDWORD
namedVirtualTerminalLevel
and set it to0x1
; then restart cmd.exe. -- You can test it with the following powershell"?[1;31mele ?[32mct ?[33mroni ?[35mX ?[36mtar ?[m".Replace('?', [char]27);
. -
Franco Rondini over 5 yearsThanks @brainslugs83 - Answer updated to cite your comment
-
Ken Ingram over 5 yearsI tried this and then docker-machine refused to connect.
-
whatisit over 5 yearsIf 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 over 5 yearsDoes someone have a source or know what
os.system('')
changes to the console ? -
Arung Isyadi almost 5 yearsI'm sorry for so long but the method above was never tested for docker.
-
Youssef13 over 4 yearsIs this supported in all Windows 10 versions ? Or available from a specific update ?
-
Daniel De León over 4 yearsin the firsts version of Windows 10 is not supported, but in the currents one work fine.
-
Youssef13 over 4 yearsI want to know the specific version that supported this.
-
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β almost 3 years@Youssef13 According to superuser.com/a/1300251/702169, first supporting version is build 16257.
-
Andry over 2 yearsJust for instance, you can recompile the app by using the
ParseAndPrintString
function directly.