
The sample code below writes the escape sequence for red, followed by the text Hello World, and finally the escape sequence to reset the terminal colours.Ĭonsole. Let’s put this in practice with an elementary. It contains a lot more in-depth information on using ANSI codes. I strongly recommend that you also read Build your own Command Line with ANSI escape codes. Finally, you will probably need to reset the colour and for that, you will write the sequence ESC[0m to the terminal. Finally, we need to close off the sequence with m.Īny text following this will now be printed in red. Let’s say, for example, that we want to switch the terminal foreground colour to red, we can write the escape sequence ESC[31m to the terminal.Įscape sequences for colours always start off with ESC[, followed by the number for the colour, in this case, 31, which is red. To tell the terminal to switch to using a specific colour we need to send the escape sequence for that colour. As discussed in the introduction, it allows you to perform various actions in the terminal, but for now, we’ll focus on the use case for printing text in specific colours. A brief overview of using the ANSI codesĪNSI escape codes are character sequences which you can print to the terminal (or console) window to give the terminal certain instructions. Please check it out if you want to use these techniques. Update on 22 October 2018: Manuel Riezebosch notified me of a library he wrote called Crayon that wraps the techniques I describe in this blog posts.

I will also discuss why you should probably avoid using them - especially when developing applications for Windows. In this blog post, I will look specifically at the colour codes and how you can use them in. They allow you to do things such as move the cursor around, displaying text in different colours, display text in bold, underline, etc, as well as various other things.


ANSI escape codes allow you to perform a range of manipulations in console applications.
