63

Is there any console emulator for Windows that interprets ANSI coloring?

We use rspec and a part of our team use Windows and the special string for coloring are not very useful for them ;).

mrzasa
  • 755
  • 2
  • 8
  • 7
  • Same question on SO: [batch file - How to echo with different colors in the Windows command line - Stack Overflow](https://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line/38617204#38617204) – user202729 Apr 13 '23 at 04:26

8 Answers8

84

None of the answers on this page mention an important aspect of the new support for ANSI Terminal Control which was added to the Windows 10 console host in build 16257 (and later). Namely, it's n̲o̲t̲ e̲n̲a̲b̲l̲e̲d̲ by default. Unless the specific software you're using enables ANSI processing by calling the SetConsoleMode API with the ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x0004) flag, you won't see colors or get ANSI processing for that application.

ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
https://docs.microsoft.com/windows/console/setconsolemode

When writing with WriteFile or WriteConsole, characters are parsed for VT100 and similar control character sequences that control cursor movement, color/font mode, and other operations that can also be performed via the existing Console APIs. For more information, see Console Virtual Terminal Sequences.

I'm guessing the reason the examples shown on this page work is that, apparently the echo command (or perhaps the batch-processing part of CMD.EXE or conhost) enables ANSI processing by calling the API just mentioned. But many other tools or executables that write to stdin or stdout might not do this, in which case you won't see color for those processes.

Fortunately, the global default can be changed from opt-in to opt-out. The registry key at HKEY_CURRENT_USER\Console\VirtualTerminalLevel sets the global default behavior for processing ANSI escape sequences. Create a DWORD key (if necessary) and set its value to 1 to globally enable (or 0 to disable`) ANSI processing by default.

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

Note that this registry setting controls a default, meaning that it only affects console apps which don't explicitly manipulate the console mode by calling SetConsoleMode(...). It follows that, while the registry value may help enable ANSI for console-mode-oblivious apps, it will have no effect on any console-mode-savvy app which (for some reason) may explicitly disable ANSI.

enter image description here

Glenn Slayden
  • 1,465
  • 12
  • 20
  • 5
    Note `VirtualTerminalLevel` breaks stuff like `git status` for PowerShell 5.1. Workaround is https://github.com/microsoft/terminal – Zombo Jun 14 '20 at 00:37
  • 5
    In Admin *Powershell*, just use: `Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Type DWORD 1`. – not2qubit Dec 30 '21 at 20:18
  • Is it possible to enable ANSI colors via BAT script? without programming and possibly without admin rights? – luca.vercelli Jan 10 '23 at 08:34
  • 1
    @luca.vercelli Yes, you can use the `reg.exe` tool in a batch file to effect the change shown above. – Glenn Slayden Jan 10 '23 at 09:59
50

ConEmu supports ANSI X3.64 with Xterm 256 color extension. I'm the author of this console emulator.

Ansi sequences are handled for all processes, running in ConEmu tabs.

AFAIK, ConEmu supports more codes, than Ansicon.

ConEmu and ANSI X3.64 / Xterm 256 colors

Maximus
  • 20,637
  • 16
  • 91
  • 116
25

Starting from Windows 10 TH2 (v1511), conhost.exe (and, by extension, cmd.exe) support ANSI Escape Sequences, in particular colors:

image from the MSDN page mentioned below

The MSDN page about Console Virtual Terminal Sequences explains what sequences are supported and how to enable them:

You can use GetConsoleMode and SetConsoleMode flags to configure this behavior. [...]

The behavior of the following sequences is based on the VT100 and derived terminal emulator technologies, most specifically the xterm terminal emulator. More information about terminal sequences can be found at http://vt100.net and at http://invisible-island.net/xterm/ctlseqs/ctlseqs.html.

rolve
  • 514
  • 5
  • 11
  • 4
    The supported sequences are described at https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx – dbenham Jul 07 '16 at 02:15
  • You can also change the default colors using [Colortool](https://blogs.msdn.microsoft.com/commandline/2017/08/11/introducing-the-windows-console-colortool/). New Windows 10 installs will get slightly modified colours that are easier to read (especially blue on black), but updates will keep the old, hard-to-read colours. –  Sep 12 '18 at 18:11
16

Is there any console emulator for Windows that interprets ANSI coloring?

Windows before 10 - no native support for ANSI colors on the console

For Windows version below 10, the Windows command console doesn't support output coloring by default. You could install either Cmder, ConEmu, ANSICON or Mintty (used by default in GitBash and Cygwin) to add coloring support to your Windows command console.

Windows 10 - Command Line Colors

Starting from Windows 10 the Windows console support ANSI Escape Sequences and some colors by default.

MSDN Documentation

Demo

enter image description here

Batch Command

The win10colors.cmd was written by Michele Locati:

@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.
echo [101;93m NORMAL FOREGROUND COLORS [0m
echo ^<ESC^>[30m [30mBlack[0m (black)
echo ^<ESC^>[31m [31mRed[0m
echo ^<ESC^>[32m [32mGreen[0m
echo ^<ESC^>[33m [33mYellow[0m
echo ^<ESC^>[34m [34mBlue[0m
echo ^<ESC^>[35m [35mMagenta[0m
echo ^<ESC^>[36m [36mCyan[0m
echo ^<ESC^>[37m [37mWhite[0m
echo.
echo [101;93m NORMAL BACKGROUND COLORS [0m
echo ^<ESC^>[40m [40mBlack[0m
echo ^<ESC^>[41m [41mRed[0m
echo ^<ESC^>[42m [42mGreen[0m
echo ^<ESC^>[43m [43mYellow[0m
echo ^<ESC^>[44m [44mBlue[0m
echo ^<ESC^>[45m [45mMagenta[0m
echo ^<ESC^>[46m [46mCyan[0m
echo ^<ESC^>[47m [47mWhite[0m (white)
echo.
echo [101;93m STRONG FOREGROUND COLORS [0m
echo ^<ESC^>[90m [90mWhite[0m
echo ^<ESC^>[91m [91mRed[0m
echo ^<ESC^>[92m [92mGreen[0m
echo ^<ESC^>[93m [93mYellow[0m
echo ^<ESC^>[94m [94mBlue[0m
echo ^<ESC^>[95m [95mMagenta[0m
echo ^<ESC^>[96m [96mCyan[0m
echo ^<ESC^>[97m [97mWhite[0m
echo.
echo [101;93m STRONG BACKGROUND COLORS [0m
echo ^<ESC^>[100m [100mBlack[0m
echo ^<ESC^>[101m [101mRed[0m
echo ^<ESC^>[102m [102mGreen[0m
echo ^<ESC^>[103m [103mYellow[0m
echo ^<ESC^>[104m [104mBlue[0m
echo ^<ESC^>[105m [105mMagenta[0m
echo ^<ESC^>[106m [106mCyan[0m
echo ^<ESC^>[107m [107mWhite[0m
echo.
echo [101;93m COMBINATIONS [0m
echo ^<ESC^>[31m                     [31mred foreground color[0m
echo ^<ESC^>[7m                      [7minverse foreground ^<-^> background[0m
echo ^<ESC^>[7;31m                   [7;31minverse red foreground color[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m
Jens A. Koch
  • 361
  • 3
  • 6
  • As of Version 1607 (OS Build 14393.693), the color in Windows 10 has been disabled. – geff_chang Feb 22 '17 at 14:20
  • 3
    Why should i believe that? Facts please. a) There is nothing in the changelog: https://support.microsoft.com/en-us/help/4009938 b) Works for me: http://imgur.com/a/MNPNm – Jens A. Koch Feb 22 '17 at 14:32
  • 1
    I'm not sure in what exact Win10 version, but it's mentioned here: https://github.com/symfony/symfony/issues/19520 On my company laptop -- Ver 1511 (OS Build 10586.753) -- I didn't need it. On my home PC, I had to use ansicon, because the colors weren't on by default. – geff_chang Feb 23 '17 at 12:11
  • 4
    They just introduced a flag, which controls colored output and is false by default. See https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on-windo/suggestions/15617610--re-enable-enable-virtual-terminal-processing-by) --- Symfony is using PHP.exe on Windows, where the flag was off, so they had to wait for a patch, which landed here: https://github.com/php/php-src/pull/2103. Situation resolved. --- For cmd.exe the flag is true always. That's why you have color support on the console. – Jens A. Koch Feb 23 '17 at 12:49
  • 1
    would these codes work in a shell script in linux too? or is that done a different way? – Mikey Aug 16 '17 at 05:23
  • @Mikey http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html – Elder Geek Sep 28 '17 at 13:42
  • @ElderGeek that's hardly an answer to his question. He's wondering (I think) if these escape sequences are the same across environments. I am too, a simple yes or no would work but I don't care enough to diff the two environments :) – Adam Plocher Jan 22 '18 at 09:05
  • @AdamPlocher IMHO, It's more effective to teach a man to fish than to give him a fish. Since when is a comment an answer anyway? – Elder Geek Jan 22 '18 at 13:22
  • @geff_chang Excellent explanation of changes to Windows 10 behavior can be found at https://github.com/Microsoft/WSL/issues/1173#issuecomment-254250445. – Glenn Slayden Mar 06 '18 at 05:02
  • fyi if you're looking to use colors in a custom prompt, `$e` expands to an escape character. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/prompt – starwarswii Oct 06 '20 at 04:27
8

Currently CMD console colors are not enabled by default in Win10, so, to enable it add this to your code:

#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD dwMode = 0;
    GetConsoleMode(hOut, &dwMode);
    dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(hOut, dwMode);

    // References:
    //SetConsoleMode() and ENABLE_VIRTUAL_TERMINAL_PROCESSING?
    //https://stackoverflow.com/questions/38772468/setconsolemode-and-enable-virtual-terminal-processing

    // Windows console with ANSI colors handling
    // https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling
#endif
phuclv
  • 26,555
  • 15
  • 113
  • 235
Pedro77
  • 1,861
  • 8
  • 31
  • 41
3

Windows Terminal has support for ANSI escape codes:

https://github.com/microsoft/terminal

It is open source, and you can also download builds from here:

https://github.com/microsoft/terminal/releases

Zombo
  • 1
  • 24
  • 120
  • 163
1

Here is the code to safely enable Console Virtual Terminal Sequences in your own programs. It's more or less a cut and paste of the code from the Microsoft site with a couple of cursor position control codes on the end.

#include<iostream>
#include<windows.h>

using namespace std;

int main()
{

    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if (hOut == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);

    if (hIn == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    DWORD dwOriginalOutMode = 0;
    DWORD dwOriginalInMode = 0;

    if (!GetConsoleMode(hOut, &dwOriginalOutMode))
    {
        return false;
    }

    if (!GetConsoleMode(hIn, &dwOriginalInMode))
    {
        return false;
    }

    DWORD dwRequestedOutModes = ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
    DWORD dwRequestedInModes = ENABLE_VIRTUAL_TERMINAL_INPUT;

    DWORD dwOutMode = dwOriginalOutMode | dwRequestedOutModes;

    if (!SetConsoleMode(hOut, dwOutMode))
    {
        return -1;
    }

    DWORD dwInMode = dwOriginalInMode | ENABLE_VIRTUAL_TERMINAL_INPUT;

    if (!SetConsoleMode(hIn, dwInMode))
    {
        return -1;
    }


    cout << "blink";


    cout << "\x1B[10;20fHello";


    cout << "\x1B[11;20fWorld" << endl;
}
ZygD
  • 2,459
  • 12
  • 26
  • 43
0

Although you can enable ANSI encoding using the above methods, but it comes with its own limitation. Instead of going through hefty processes, you can use Git Bash, which encodes all of the ANSI colors, and its easy to use.

  • 1
    gitbash uses msys, which changes the shell, not only the console. that's not what people asked. Just use the new Windows Terminal app from the Windows Store. – Luiz Felipe Jun 03 '21 at 04:22