Skip to content

Instantly share code, notes, and snippets.

@mlocati
Last active April 29, 2024 20:33
Show Gist options
  • Save mlocati/fdabcaeb8071d5c75a2d51712db24011 to your computer and use it in GitHub Desktop.
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
@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