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 theC code level, in order that the ANSI.SYS driver is loaded and the ANSI/VT100 escape sequences recognized?
- 4there is
coloramamodule for Python:"On Windows, Colorama strips these ANSI characters from stdout and converts them into equivalent win32 calls for colored text. On other platforms, Colorama does nothing."jfs– jfs2017-09-18 08:25:08 +00:00CommentedSep 18, 2017 at 8:25 - 3Please notice that the rules of the game have changed drastically as of recent versions of Windows 10.Andreas Rejbrand– Andreas Rejbrand2018-07-26 13:37:15 +00:00CommentedJul 26, 2018 at 13:37
- 7FYI, in latest Windows 10, you can enable ANSI in conhost via the following reghack -- in
HKCU\Consolecreate aDWORDnamedVirtualTerminalLeveland 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– BrainSlugs832018-10-27 21:17:02 +00:00CommentedOct 27, 2018 at 21:17 - 2updlearn.microsoft.com/en-us/windows/console/… here how it could be enabled without editing registry, and possibly breaking other applicationsBohdan Mart– Bohdan Mart2019-03-08 18:13:00 +00:00CommentedMar 8, 2019 at 18:13
- 1@BrainSlugs83 Your suggestion doesn't appear to work with Windows 10 V22H2 (OS Build 19045.2604). Ugh! MS!ScottWelker– ScottWelker2023-03-30 21:39:45 +00:00CommentedMar 30, 2023 at 21:39
14 Answers14
[UPDATE] For latest Windows 10 please read useful contribution by @brainslugs83, just below in the comments to this answer.
While for versions beforeWindows 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 theWikipedia 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 asAnsicon can however act as a wrapper around the standard Win32 console and add support for ANSI escape sequences.
So I thinkANSICON by Jason Hood is your solution. It is written inC, supports 32-bit and 64-bit versions of Windows, andthe source is available.
Also I found some other similar question or post which ultimately have been answered to use ANSICON:
6 Comments
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);.Starting from Windows 10 TH2 (v1511),conhost.exe andcmd.exe support ANSI and VT100 Escape Sequences out of the box (althoughthey have to be enabled).
Seemy answer over at superuser for more details.
2 Comments
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);.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 14 Comments
For Python 2.7 the following script works for me fine with Windows 10 (v1607)
import osprint '\033[35m'+'color-test'+'\033[39m'+" test end"os.system('') #enable VT100 Escape Sequence for WINDOWS 10 Ver. 1607print '\033[35m'+'color-test'+'\033[39m'+" test end"Result should be:
[35mcolor-test[39m test endcolor-test test end5 Comments
(...) this works in Python 3 on Windows 10 (v1703). (i.e.print('\033[35m'+'color-test'+'\033[39m'+" test end"))os.system('') changes to the console ?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 onissue 30075 on python.org\033[0m. Or, if you want to reset the whole terminal, and clear the screen, do\033c.Starting from Windows 10, you can useENABLE_VIRTUAL_TERMINAL_PROCESSING to enable ANSI escape sequences:
https://learn.microsoft.com/windows/console/console-virtual-terminal-sequences
Comments
If ANSICON is not acceptable since it requires you to install something on the system, a more lightweight solution that parses and translates the ANSI codes into the relevantWin32 API console functions such asSetConsoleTextAttribute.
1 Comment
ParseAndPrintString function directly.For coloring the cmd you needWindows.h and useSetConsoleTextAttribute() more details can be found inhttps://learn.microsoft.com/windows/console/setconsoletextattribute
1 Comment
In lastest win10, it can be done bySetConsoleMode(originMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING). Seehttps://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example
Comments
MaybeANSICON can help u
Just download and extract files, depending on your windows os: 32bit or 64bit
Install it with:ansicon -i
Comments
I personally likeclink. It not only processes ANSI codes, it also adds many other features so Windows Console behaves likebash (history, reverse history search, keyboard shortcuts, etc.):
- The same line editing as Bash (from GNU's Readline library).
- History persistence between sessions.
- Context sensitive completion;
- Executables (and aliases).
- Directory commands.
- Environment variables
- Thirdparty tools; Git, Mercurial, SVN, Go, and P4.
- New keyboard shortcuts;
- Paste from clipboard (Ctrl-V).
- Incremental history search (Ctrl-R/Ctrl-S).
- Powerful completion (TAB).
- Undo (Ctrl-Z).
- Automatic "cd .." (Ctrl-PgUp).
- Environment variable expansion (Ctrl-Alt-E).
- (pressAlt-H for many more...)
- Scriptable completion with Lua.
- Coloured and scriptable prompt.
- Auto-answering of the "Terminate batch job?" prompt.
Comments
Ansi.sys (in the system32 folder) is an "MSDOS driver" provided as part of Windows XP, 2000, and earlier versions of NT. In 2000 and XP, it is located in the system32 folder (I don't remember the structure of earlier versions of NT). Programs that run in the DOS subsystem and use standard output can use ANSI.SYS just as they could running over MSDOS.
To load ansi.sys, you must use the device= or devicehigh= command in config, just as you would in MSDOS. On Windows NT 5 (2K & XP), each copy of the DOS subsystem can be given a separate config file in the pif/shortcut (use the "advanced" button), and there is a default file called CONFIG.NT (also in the system32 folder), which is used if the pif/shortcut does not specify a special config file.
When ansi.sys is loaded correctly, mem /d will report that it is loaded. On earlier versions of NT, you can and must load a proper DOS environment to load ansi.sys, and ansi art will work at the prompt. On Win 2K and XP, loading ansi.sys will have no effect on your "CMD prompt" because CMD is not a DOS program: it is a 32 bit Windows console program. For some reason that I do not understand, on WinXP, even if you load a fixed copy of command.com using "command.com /p", the command prompt will not be ansi enabled: perhaps when you do it that way it only emulates loading command.com?
In any case, when you use an actual DOS version of command.com, ansiis enabled after being loaded: you can demonstrate it's use with a bit of ansi art like this:
command /c type ansiart.ans(here is an example:http://artscene.textfiles.com/ansi/artwork/beastie.ans)
CONFIG.NT (in the system32 folder) contains an example of the syntax for loading device drivers. You will need to be an Administrator to edit that default file, or you can make a copy of it.
On Win 2K and XP, the default "shortcut" for MSDOS is a .PIF file, not a .LNK file. If you create a .lnk file to CMD, you won't be able to set special config and autoexec files, it will use the default CONFIG.NT. If you want to use a special config file for just one DOS application, you can make a copy of the "MSDOS shortcut", or you can make a copy of "_default.pif", found in your Windows folder.
Comments
I found this tool to be working for my end.Microsoft Color Tool from GitHub
Unzip the compressed file then open CMD with Administration permission.
Go to the folder where you unzip the file in CMD.
Then execute this command "colortool -b scheme-name"
The scheme-name needs to be replaced with any of these options below:
- campbell.ini
- campbell-legacy.ini
- cmd-legacy.ini
- deuternopia.itermcolors
- OneHalfDark.itermcolors
- OneHalfLight.itermcolors
- solarized_dark.itermcolors
- solarized_light.itermcolors
In my case, the command would be like this "colortool -b solarized_dark.itermcolors"
Click right on the console window and select Properties.
You don't need to change any value just click "OK" to save the setting. (You will notice that your font already contains colors).
Then restart your cmd or powerShell.
The ANSI color should be enabled and working with the color scheme you chose before.
2 Comments
Had the same issue. I installedConEmu and that one solved my problem.
Comments
Somehow in Windows you just need to call any shell command first, rather call thesystem function. Just in start of your main method putsystem("");, and don't forget to includestdlib.h.
I noticed this when I looked at some of my old programs that also used ANSI codes to understand why they work, but my new code is not
Comments
Explore related questions
See similar questions with these tags.










