Skip to Content

What is color code in CMD?

Welcome back readers! Today we will be diving into the world of color coding in the Windows command prompt. Color coding can help make output from commands much easier to read and understand. There are several different color codes that can be used to customize the text and background colors. Stick with me as we go through what color codes are, how to use them, and some examples of how they can be helpful. Let’s get started!

What are color codes?

Color codes are special codes that can be used within the Windows command prompt to change the text and background colors. The command prompt typically just displays white text on a black background. But with color codes, you can customize this to use different color combinations.

Color codes are specified using special escape sequences that start with an escape character. For the command prompt, this escape character is the caret ^. After the escape character, you specify codes for the text and background color, separated by a semicolon. Here is the basic structure:

^;m

The text color code determines the color of the text that follows. The background color code sets the background color. The m at the end signals the end of the color code escape sequence.

Some examples:

^[[1;31m = Red text on white background

^[[0;42m = Black text on green background

^[[1;44m = Blue text on blue background

There are standard color codes from 0-15 that map to specific colors which we will cover next.

Standard Color Codes

Here are the standard color codes that can be used for both the text and background colors:

Color Code Color
0 Black
1 Blue
2 Green
3 Aqua
4 Red
5 Purple
6 Yellow
7 White
8 Gray
9 Light Blue
10 Light Green
11 Light Aqua
12 Light Red
13 Light Purple
14 Light Yellow
15 Bright White

So for example, to get green text on a black background you would use the code ^[[2;0m. For purple text on a white background, you would use ^[[5;7m.

Resetting Colors

You can reset back to the default white on black colors by using the code ^[0m. This will reset both the text and background colors. Resetting is useful if you want to apply color codes to only certain text and then go back to normal.

Applying Color Codes

To apply a color code, simply insert the escape sequence before the text you want to color. For example:

^[[1;44mThis text will be blue on blue^[[0m Back to normal now

The color code applies to all text following it until a reset code is encountered or the command prompt refreshes. You can combine multiple color codes to mix things up:

^[[1;44mBlue on blue ^[[2;40mNow green on black ^[[0m Back to normal

Automating with Color Codes

One very useful application of color coding is for automating scripts and batch files. You can use color codes to highlight important information, warnings, or errors.

For example, a script could display status messages in green, warnings in yellow, and errors in red:

^[[2;40mStatus: Processing file 123^[[0m

^[[3;40mWarning: File 456 not found^[[0m

^[[4;40mError: Unable to copy file^[[0m

This makes it much easier to spot issues compared to everything being white on black. You can also get creative with section headers, summaries, and other formatting to make script output cleaner.

Examples

Here are a few other examples of using color coding in the command prompt:

List files in a directory with sizes in KB. Display directories in blue and files in green:

dir /-p | findstr “^[d-]” ^[[1;44m^&^[[0m | findstr “^[^d-]” ^[[2;40m^&^[[0m

Display the current date and time in yellow:

^[[3;40m%date% %time%^[[0m

Create section headers in a script in light purple:

^[[13;40mStarting processing…^[[0m

^[[13;40mCopying files…^[[0m

^[[13;40mCleaning up…^[[0m

Limitations

There are a couple limitations to be aware of with color codes in CMD:

  • They only work in CMD on Windows, not in other terminal emulators
  • Some tools/languages within CMD may not handle or display the codes properly
  • Codes must be inserted each time CMD refreshes, they won’t persist on their own
  • The available colors are limited to the basic 16

So keep these in mind when deciding how and where to implement color coding.

Conclusion

In summary, color codes are a great way to customize and enhance the default CMD interface. They allow you to color text, background, and apply formatting. Using them appropriately can make output more readable for both interactively working in CMD as well as automating batch scripts.

The codes are specified starting with the caret escape character ^, then color numbers for text and background, and ending with m. Standard color numbers 0-15 map to common colors. Reset to default with ^[0m.

Give it a try in your own CMD prompts and scripts. Let me know in the comments if you have any other examples or tips for using color codes!