Using ANSI Escape Sequences for Colored Output in Windows Command Line

Nov 19, 2025 · Programming · 11 views · 7.8

Keywords: Windows | Batch File | Command Line | Colors | ANSI Escape Sequences

Abstract: This article provides an in-depth exploration of how to output single-line colored text in the Windows command line using ANSI escape sequences. It covers native support in Windows 10 and later, solutions for older versions with third-party tools like ANSICON, and includes rewritten batch code examples. Based on Q&A data and reference articles, the content offers detailed analysis and step-by-step guidance to help developers master command-line color control effectively.

Introduction to Colored Output in Command Line

In programming and scripting, colored output in command-line interfaces can greatly enhance user experience and debugging efficiency. This article focuses on methods to achieve single-line text color changes in the Windows environment, specifically for batch files, rather than setting the entire window color.

Overview of ANSI Escape Sequences

ANSI escape sequences are standardized control sequences for terminal text formatting, including colors, styles, and cursor control. They start with an escape character (ASCII 27), followed by specific codes. For example, <ESC>[31m sets the foreground color to red, while <ESC>[0m resets all attributes.

Differences in Windows Command Line Color Support

Starting from Windows 10, the command console natively supports ANSI escape sequences, thanks to the Threshold 2 update. Users can directly use these sequences in batch files without additional tools. For versions before Windows 10, such as Windows 7 or XP, native support is lacking, and third-party tools like Cmder, ConEmu, ANSICON, or Mintty are required to enable color functionality.

Rewritten Code Examples

Below is a batch file example for Windows 10 and later, demonstrating the use of ANSI escape sequences for colored text output. The code is rewritten based on core concepts to ensure clarity and accessibility.

@echo off
cls
echo <ESC>[101;93m STYLES <ESC>[0m
echo <ESC>[0m Reset <ESC>[0m
echo <ESC>[1m Bold <ESC>[0m
echo <ESC>[4m Underline <ESC>[0m
echo <ESC>[7m Inverse <ESC>[0m
echo.
echo <ESC>[101;93m NORMAL FOREGROUND COLORS <ESC>[0m
echo <ESC>[30m Black <ESC>[0m
echo <ESC>[31m Red <ESC>[0m
echo <ESC>[32m Green <ESC>[0m
echo <ESC>[33m Yellow <ESC>[0m
echo <ESC>[34m Blue <ESC>[0m
echo <ESC>[35m Magenta <ESC>[0m
echo <ESC>[36m Cyan <ESC>[0m
echo <ESC>[37m White <ESC>[0m
echo.
echo <ESC>[101;93m NORMAL BACKGROUND COLORS <ESC>[0m
echo <ESC>[40m Black <ESC>[0m
echo <ESC>[41m Red <ESC>[0m
echo <ESC>[42m Green <ESC>[0m
echo <ESC>[43m Yellow <ESC>[0m
echo <ESC>[44m Blue <ESC>[0m
echo <ESC>[45m Magenta <ESC>[0m
echo <ESC>[46m Cyan <ESC>[0m
echo <ESC>[47m White <ESC>[0m
echo.
echo <ESC>[101;93m STRONG FOREGROUND COLORS <ESC>[0m
echo <ESC>[90m White <ESC>[0m
echo <ESC>[91m Red <ESC>[0m
echo <ESC>[92m Green <ESC>[0m
echo <ESC>[93m Yellow <ESC>[0m
echo <ESC>[94m Blue <ESC>[0m
echo <ESC>[95m Magenta <ESC>[0m
echo <ESC>[96m Cyan <ESC>[0m
echo <ESC>[97m White <ESC>[0m
echo.
echo <ESC>[101;93m STRONG BACKGROUND COLORS <ESC>[0m
echo <ESC>[100m Black <ESC>[0m
echo <ESC>[101m Red <ESC>[0m
echo <ESC>[102m Green <ESC>[0m
echo <ESC>[103m Yellow <ESC>[0m
echo <ESC>[104m Blue <ESC>[0m
echo <ESC>[105m Magenta <ESC>[0m
echo <ESC>[106m Cyan <ESC>[0m
echo <ESC>[107m White <ESC>[0m
echo.
echo <ESC>[101;93m COMBINATIONS <ESC>[0m
echo <ESC>[31m red foreground color <ESC>[0m
echo <ESC>[7m inverse foreground <-> background <ESC>[0m
echo <ESC>[7;31m inverse red foreground color <ESC>[0m
echo <ESC>[7m and nested <ESC>[31m before nested <ESC>[0m
echo <ESC>[31m and nested <ESC>[7m before nested <ESC>[0m

Note: In actual batch files, <ESC> should be replaced with the actual escape character (ASCII 27). In Windows, this can be input using specific editors or represented via escape sequences.

Supplemental Third-Party Tools

For older Windows versions, tools like ANSICON can enable ANSI support. Reference articles indicate that users need to run ANSICON first before executing batch files. Example code:

@echo off
ansicon.exe
echo <ESC>[31m This line is Red <ESC>[0m
echo <ESC>[34m This line is Blue <ESC>[0m
echo <ESC>[37m This line is White <ESC>[0m
pause

This method has been tested on Windows 7 and XP, but it requires proper installation and invocation of ANSICON.

Best Practices and Considerations

When using ANSI escape sequences, it is advisable to test for compatibility in the target environment. For batch files, avoid direct copy-pasting of code, as escape characters may be lost. Use editors that support special characters or generate escape sequences dynamically via scripts. Microsoft's ColorTool project can be used to customize console color schemes, further enhancing flexibility.

Conclusion

By leveraging ANSI escape sequences, developers can efficiently implement colored text output in the Windows command line. Modern Windows versions simplify this process, while older systems require auxiliary tools. The code and explanations provided in this article aim to help readers get started quickly and avoid common pitfalls.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.