Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ANSI Colors in standard Windows 10 shell
@echo off
setlocal
call :setESC
cls
echo %ESC%[101;93m STYLES %ESC%[0m
echo ^<ESC^>[0m %ESC%[0mReset%ESC%[0m
echo ^<ESC^>[1m %ESC%[1mBold%ESC%[0m
echo ^<ESC^>[4m %ESC%[4mUnderline%ESC%[0m
echo ^<ESC^>[7m %ESC%[7mInverse%ESC%[0m
echo.
echo %ESC%[101;93m NORMAL FOREGROUND COLORS %ESC%[0m
echo ^<ESC^>[30m %ESC%[30mBlack%ESC%[0m (black)
echo ^<ESC^>[31m %ESC%[31mRed%ESC%[0m
echo ^<ESC^>[32m %ESC%[32mGreen%ESC%[0m
echo ^<ESC^>[33m %ESC%[33mYellow%ESC%[0m
echo ^<ESC^>[34m %ESC%[34mBlue%ESC%[0m
echo ^<ESC^>[35m %ESC%[35mMagenta%ESC%[0m
echo ^<ESC^>[36m %ESC%[36mCyan%ESC%[0m
echo ^<ESC^>[37m %ESC%[37mWhite%ESC%[0m
echo.
echo %ESC%[101;93m NORMAL BACKGROUND COLORS %ESC%[0m
echo ^<ESC^>[40m %ESC%[40mBlack%ESC%[0m
echo ^<ESC^>[41m %ESC%[41mRed%ESC%[0m
echo ^<ESC^>[42m %ESC%[42mGreen%ESC%[0m
echo ^<ESC^>[43m %ESC%[43mYellow%ESC%[0m
echo ^<ESC^>[44m %ESC%[44mBlue%ESC%[0m
echo ^<ESC^>[45m %ESC%[45mMagenta%ESC%[0m
echo ^<ESC^>[46m %ESC%[46mCyan%ESC%[0m
echo ^<ESC^>[47m %ESC%[47mWhite%ESC%[0m (white)
echo.
echo %ESC%[101;93m STRONG FOREGROUND COLORS %ESC%[0m
echo ^<ESC^>[90m %ESC%[90mWhite%ESC%[0m
echo ^<ESC^>[91m %ESC%[91mRed%ESC%[0m
echo ^<ESC^>[92m %ESC%[92mGreen%ESC%[0m
echo ^<ESC^>[93m %ESC%[93mYellow%ESC%[0m
echo ^<ESC^>[94m %ESC%[94mBlue%ESC%[0m
echo ^<ESC^>[95m %ESC%[95mMagenta%ESC%[0m
echo ^<ESC^>[96m %ESC%[96mCyan%ESC%[0m
echo ^<ESC^>[97m %ESC%[97mWhite%ESC%[0m
echo.
echo %ESC%[101;93m STRONG BACKGROUND COLORS %ESC%[0m
echo ^<ESC^>[100m %ESC%[100mBlack%ESC%[0m
echo ^<ESC^>[101m %ESC%[101mRed%ESC%[0m
echo ^<ESC^>[102m %ESC%[102mGreen%ESC%[0m
echo ^<ESC^>[103m %ESC%[103mYellow%ESC%[0m
echo ^<ESC^>[104m %ESC%[104mBlue%ESC%[0m
echo ^<ESC^>[105m %ESC%[105mMagenta%ESC%[0m
echo ^<ESC^>[106m %ESC%[106mCyan%ESC%[0m
echo ^<ESC^>[107m %ESC%[107mWhite%ESC%[0m
echo.
echo %ESC%[101;93m COMBINATIONS %ESC%[0m
echo ^<ESC^>[31m %ESC%[31mred foreground color%ESC%[0m
echo ^<ESC^>[7m %ESC%[7minverse foreground ^<-^> background%ESC%[0m
echo ^<ESC^>[7;31m %ESC%[7;31minverse red foreground color%ESC%[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m %ESC%[7mbefore %ESC%[31mnested%ESC%[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m %ESC%[31mbefore %ESC%[7mnested%ESC%[0m
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
exit /B 0
@mlocati

This comment has been minimized.

Copy link
Owner Author

mlocati commented Jun 15, 2016

Output:

output

@Tymski

This comment has been minimized.

Copy link

Tymski commented Jul 16, 2017

Cool

@rourke

This comment has been minimized.

Copy link

rourke commented Dec 13, 2017

@mlocati Thanks for this! However it doesn't work in a for loop:

image

Do you know how to solve this?

@AndyK70

This comment has been minimized.

Copy link

AndyK70 commented Jan 9, 2018

@rourke it works in a loop, try
grafik

But if you put in timeout it works only for the first loop pass. Maybe there is something about this command which breaks the coloring codes:
grafik

If you go with pause it works:
grafik

if you only want the time delay but no user interaction you should go with the oldstyle ping:
grafik

@mistergoliath41

This comment has been minimized.

@xuanchuong

This comment has been minimized.

Copy link

xuanchuong commented Dec 28, 2018

very nice (y)

@T0MuX

This comment has been minimized.

Copy link

T0MuX commented Feb 1, 2019

Not working for me too. Win7 SP1 NT6.1.7601.
I type directly thoses commands in cmd.exe it even no copying right.

@ACBob

This comment has been minimized.

Copy link

ACBob commented Mar 20, 2019

Thank you for this.
Now it's easier for me to identify my error codes!

@hXR16F

This comment has been minimized.

Copy link

hXR16F commented May 21, 2019

Very helpful
Screenshot_1

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 10, 2019

@MG-Tech @T0MuX This is for Windows 10 (Build 16257 and higher)

@tuanla07

This comment has been minimized.

Copy link

tuanla07 commented Aug 24, 2019

@MG-Tech @T0MuX This is for Windows 10 (Build 16257 and higher)

How can we try it with Win7 & WinXP?

@mlocati

This comment has been minimized.

Copy link
Owner Author

mlocati commented Aug 24, 2019

How can we try it with Win7 & WinXP?

@tuanla07 those versions of Windows don't support this feature

@hXR16F

This comment has been minimized.

Copy link

hXR16F commented Sep 2, 2019

@tuanla07 By using ansicon.

@Dr-WaSaBi

This comment has been minimized.

Copy link

Dr-WaSaBi commented Oct 4, 2019

Can anyone tell me how to enter an escape code using VS Code?

@mlocati

This comment has been minimized.

Copy link
Owner Author

mlocati commented Oct 4, 2019

@Dr-WaSaBi the usual way to enter the ESC ascii character is to hold down the ALT key and press 027 on the numeric keypad (when NumLock is on).
This works in Notepad++, but it seems that VS Code doesn't like it.
You have 2 options:

  1. download this raw win10colors.cmd file, open it in VS Code, copy the escape character included, and paste it wherever you want
  2. file an issue at https://github.com/Microsoft/vscode/issues
@gothicserpent

This comment has been minimized.

Copy link

gothicserpent commented Dec 26, 2019

thank you so much for this! using this in a batch script! couldn't find the escape characters anywhere once i downloaded this I was set! Thanks again.

@gothicserpent

This comment has been minimized.

Copy link

gothicserpent commented Jan 15, 2020

@dyslexicanaboko I got it to work by simply downloading the .cmd file to my computer, and copying and pasting the breaks directly into my batch files.

so in example:

REM updates git local windows copy

echo off

:START

cls
echo.
echo Git will update from the current version:
echo.
git --version
echo.
set /p DUMMY=�[97mHit ENTER to continue...�[0m
echo.
git update-git-for-windows
echo.
set /p DUMMY=�[92mDone, Hit ENTER to continue...�[0m

updates my git install on windows.

@gothicserpent

This comment has been minimized.

Copy link

gothicserpent commented Jan 15, 2020

@Dr-WaSaBi the usual way to enter the ESC ascii character is to hold down the ALT key and press 027 on the numeric keypad (when NumLock is on).
This works in Notepad++, but it seems that VS Code doesn't like it.
You have 2 options:

  1. download this raw win10colors.cmd file, open it in VS Code, copy the escape character included, and paste it wherever you want
  2. file an issue at https://github.com/Microsoft/vscode/issues

have to perform these steps @dyslexicanaboko

@dyslexicanaboko

This comment has been minimized.

Copy link

dyslexicanaboko commented Jan 15, 2020

Just a general FYI for anyone scratching their head like I was. It may appear that if you type this directly into a command window it doesn't work. However if you run the batch file it works. Upon further inspection if you open the batch file with Notepad++ it will show you the hidden "ESC" characters which are required in order to make it work. Don't confuse this with the escape key on your keyboard, it's not the same thing. It's actually the alt code: ALT + 0 + 2 + 7

Therefore you can actually type this directly into the CMD window, it's just a pain to do because you have to use that ALT code as mentioned above. I was able to do it, it was just torture.

@dyslexicanaboko

This comment has been minimized.

Copy link

dyslexicanaboko commented Jan 16, 2020

@Dr-WaSaBi the usual way to enter the ESC ascii character is to hold down the ALT key and press 027 on the numeric keypad (when NumLock is on).
This works in Notepad++, but it seems that VS Code doesn't like it.
You have 2 options:

  1. download this raw win10colors.cmd file, open it in VS Code, copy the escape character included, and paste it wherever you want
  2. file an issue at https://github.com/Microsoft/vscode/issues

have to perform these steps @dyslexicanaboko

@gothicserpent Hey thanks for replying. I realized my error and I deleted my original post, sorry about that. I was just surprised to see hidden characters, that didn't cross my mind before. It's been a very long time since I have had to work with hidden characters. Reminds me of dealing with UART and TAP.

@mlocati

This comment has been minimized.

Copy link
Owner Author

mlocati commented Jan 16, 2020

I updated the script: now the escape character (ASCII code decimal 27, hexadecimal 1B) is assigned to the new ESC environment variable by the setESC subroutine, we don't have that invisible char anymore.

@dyslexicanaboko

This comment has been minimized.

Copy link

dyslexicanaboko commented Jan 21, 2020

I updated the script: now the escape character (ASCII code decimal 27, hexadecimal 1B) is assigned to the new ESC environment variable by the setESC subroutine, we don't have that invisible char anymore.

Thank you for doing that! That does make it easier!

@sepehrst

This comment has been minimized.

Copy link

sepehrst commented Jun 1, 2020

The setESC subroutine can be simplified to:

:setESC
for /F %%a in ('"prompt $E$S & echo on & for %%b in (1) do rem"') do set "ESC=%%a"
exit /B
@Pongsapat3791

This comment has been minimized.

Copy link

Pongsapat3791 commented Jun 29, 2020

@echo off
setlocal
call :setESC

Test
Test
Test

:setESC
for /F %%a in ('"prompt $E$S & echo on & for %%b in (1) do rem"') do set "ESC=%%a"
exit /B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.