Skip to content

Instantly share code, notes, and snippets.

@mlocati
Last active September 24, 2024 13:45
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
@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