Skip to content

Instantly share code, notes, and snippets.

@mlocati
Last active January 19, 2024 14:18
Star You must be signed in to star a gist
Save mlocati/fdabcaeb8071d5c75a2d51712db24011 to your computer and use it in GitHub Desktop.
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
@hXR16F
Copy link

hXR16F commented May 21, 2019

Very helpful
Screenshot_1

Copy link

ghost commented Jul 10, 2019

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

@tuanla07
Copy link

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

How can we try it with Win7 & WinXP?

@mlocati
Copy link
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
Copy link

hXR16F commented Sep 2, 2019

@tuanla07 By using ansicon.

@Dr-WaSaBi
Copy link

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

@mlocati
Copy link
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
Copy link

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
Copy link

@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
Copy link

@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
Copy link

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
Copy link

@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
Copy link
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
Copy link

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
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
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

@TheFlash2k
Copy link

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

@hXR16F
Copy link

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
Copy link

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

@Claudetom
Copy link

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
Copy link
Author

mlocati commented Dec 5, 2020

@AhmetEnesKCC
Copy link

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

@DitherDude
Copy link

Hello,

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

@AhmetEnesKCC
Copy link

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
Copy link

@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?

@PureOcean
Copy link

Thank you for the script. Thanks to these codes, I will be able to color specific words in my BAT files. Of course, by simplifying it as much as possible. An example, on CMD:

@for /F "tokens=1,2 delims=#" %a in ('"prompt #$H#$E# & echo on & for %b in (1) do rem"') do set ESC=%b
@set Yellow=%ESC%[93m
@Set ColorOff=%ESC%[0m
@Echo An attempt to show a word as %Yellow%yellow%ColorOff% color in a sentence...

@SPIKEYPUP
Copy link

Here let me repost... I'm on Win10 Pro
image
image

@SPIKEYPUP
Copy link

Here let me repost... I'm on Win10 Pro image image

Ok there was an email here stating it no longer worked, I replied to show I was not having the issue reported, perhaps they pulled their comment/request already. I'll leave this up for historical purposes I suppose lol , a little posterirty as it were.

Copy link

ghost commented Mar 14, 2022

Lite version.
https://pastebin.com/tCn7JLGJ
Open pastebin Link, Click on download.

Basic

Clean text + %<% + FOREGROUND / BACKGROUND / STYLES + Custom text + %>% + Clean text

Combine FOREGROUND / BACKGROUND / STYLES

Example: %FF_Black% + %BB_Cyan% + %Inverse%
have to Convert into this: %FF_Black:m=;%%BB_Cyan:m=;%%Inverse%
The rule. the last VAR doesn't change, only the others before him.
By replace %VAR% with %VAR:m=;%

Code

@echo off

rem ANSI Colors in standard Windows 10 shell
rem https://gist.github.com/mlocati/fdabcaeb8071d5c75a2d51712db24011

:: BASIC CHARS
:: ALT 0,2,7 --> �
:: WORK WITH NOTPAD++
set "<=�["
set ">=�[0m"

:: STYLES
set "Reset=0m" & set "Bold=1m"
set "Underline=4m" & set "Inverse=7m"

:: BACKGROUND COLORS
set "B_Black=30m" & set "B_Red=31m" & set "B_Green=32m"
set "B_Yellow=33m" & set "B_Blue=34m" & set "B_Magenta=35m"
set "B_Cyan=36m" & set "B_White=37m"
set "BB_Black=90m" & set "BB_Red=91m" & set "BB_Green=92m"
set "BB_Yellow=93m" & set "BB_Blue=94m" & set "BB_Magenta=95m"
set "BB_Cyan=96m" & set "BB_White=97m"

:: FOREGROUND COLORS
set "F_Black=40m" & set "F_Red=41m" & set "F_Green=42m"
set "F_Yellow=43m" & set "F_Blue=44m" & set "F_Magenta=45m"
set "F_Cyan=46m" & set "F_White=47m"
set "FF_Black=100m" & set "FF_Red=101m" & set "FF_Green=102m"
set "FF_Yellow=103m" & set "FF_Blue=104m" & set "FF_Magenta=105m"
set "FF_Cyan=106m" & set "FF_White=107m"

:: DEMO
echo.
echo  ONLY BACKGROUND COLORS %<%%BB_Red% RED %>% ^& %<%%BB_Green% GREEN %>%
echo  ONLY FOREGROUND COLORS %<%%FF_Magenta% MAGNETA %>% ^& %<%%F_Yellow% YELLOW %>%
echo  COMBINATION COLORS ^& STYLE %<%%FF_Black:m=;%%B_Green:m=;%%Bold%F:BLACK B:GREEN S:BOLD%>%

@armanexplorer
Copy link

armanexplorer commented Oct 28, 2022

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

+1: This is much more straightforward and understandable.
Thank you @sepehrst

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