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 mlocati commented Jun 15, 2016

Output:

output

@Tymski

This comment has been minimized.

Copy link

@Tymski Tymski commented Jul 16, 2017

Cool

@rourke

This comment has been minimized.

Copy link

@rourke 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 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 xuanchuong commented Dec 28, 2018

very nice (y)

@T0MuX

This comment has been minimized.

Copy link

@T0MuX 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 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 hXR16F commented May 21, 2019

Very helpful
Screenshot_1

@ghost

This comment has been minimized.

Copy link

@ghost 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 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 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 hXR16F commented Sep 2, 2019

@tuanla07 By using ansicon.

@Dr-WaSaBi

This comment has been minimized.

Copy link

@Dr-WaSaBi 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 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 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 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 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 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 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 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 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 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 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

@TheFlash2k

This comment has been minimized.

Copy link

@TheFlash2k TheFlash2k commented Sep 18, 2020

Is it possible to use it with "PROMPT" command to create a colorful prompt?

@hXR16F

This comment has been minimized.

Copy link

@hXR16F hXR16F commented Sep 18, 2020

Is it possible to use it with "PROMPT" command to create a colorful prompt?

Yes, see this https://github.com/hXR16F/hyperial

@SPIKEYPUP

This comment has been minimized.

Copy link

@SPIKEYPUP SPIKEYPUP commented Nov 29, 2020

Thanks for this @mlocati this was helpful for me today 👍

@Claudetom

This comment has been minimized.

Copy link

@Claudetom Claudetom commented Dec 5, 2020

Bonjour,
Dans un fichier Batch .bat,
Je pose des questions pour le renseignement de variables, j'ai choisi de coloriser le texte de la demande, mais le problème est qu'un retour à la ligne est présent, pour la saisie à la question ?
est-il possible de faire en sorte, que la réponse qui est saisie par l'utilisateur soit sur la même ligne que la question ?

English:
Hello,
In a Batch .bat file,
I ask questions for the information of variables, I chose to colorize the text of the request, but the problem is that a new line is present, for the seizure to the question?
is it possible to ensure that the answer entered by the user is on the same line as the question?

:: Demande Nom de l instance echo %ESC%[0;91m Saisissez votre Instance SQL: %ESC%[0m set /p INSTANCEC= if DEFINED INSTANCEC (SET "INSTANCE=%INSTANCEC%") ELSE (GOTO ENDPPOCESS:)

  1. Je voudrais que la réponse soit à la suite de " Saisissez votre Instance SQL: " <-- Ici la réponse
  2. Est-il possible d'afficher le texte en Bold avec un background en rouge ?

English:

  1. I would like the answer to be after "Enter your SQL Instance:" <- Here the answer
  2. Is it possible to display the text in Bold with a background in red?

Thank you for your time and your developments

Claude

@mlocati

This comment has been minimized.

Copy link
Owner Author

@mlocati mlocati commented Dec 5, 2020

@AhmetEnesKCC

This comment has been minimized.

Copy link

@AhmetEnesKCC AhmetEnesKCC commented Feb 23, 2021

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

Just Create a bat file and paste into it.
Then start with ->
start [filename].bat

@Choni01

This comment has been minimized.

Copy link

@Choni01 Choni01 commented Aug 11, 2021

Hello,

Is there a way to apply these colors to "title" to create a multi-colored title on a batch script?

@AhmetEnesKCC

This comment has been minimized.

Copy link

@AhmetEnesKCC AhmetEnesKCC commented Aug 11, 2021

Hello,

Is there a way to apply these colors to "title" to create a multi-colored title on a batch script?

What do you mean with 'title'

@SPIKEYPUP

This comment has been minimized.

Copy link

@SPIKEYPUP SPIKEYPUP commented Aug 11, 2021

@Choni01 if you mean window title (dos/batch "title" command controls window title), I don't think so because in the windows environment the window frame is controlled within the windows environment and not from the command line, so direct control may be out. You might be able to find something that can be called or triggered within your command line app that triggers a windows app to modify the title bar of the target window/box. Is that what you are thinking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment