Millet Porridge

English version of https://corvo.myseu.cn

0%

Special ASCII Characters

There’s a reason the characters in a Linux terminal can be so bright and colorful. . .

Special ASCII characters

Try it once, and you can mix and match whatever you want.

That’s right — \33[ is the key.

First, \33[1m ~ \33[8m control the character style

\33[31m ~ \33[37m control the text color

And \33[40m ~ \33[47m control the background color

This should be easy enough to follow, so here are a few more advanced ones.

\33[0m turns off all display effects. These ASCII effects can be sticky: once you use one, the subsequent printf calls keep repeating the last effect setting, so you need it to close all attributes — you’ll see when you try it.

\33[2J clears the screen

It’s very easy to experiment under Linux — take the echo command for example.

1
echo "\033[32mhello world"  

This prints green text.

green

But I suggest getting into a good habit when setting styles:

1
echo "\033[0;33mhello world\033[0m"

Yello

  • Clear the previous settings before printing, then apply your own, and clear again on exit — otherwise the last effect setting keeps repeating. Doing just one of them also works; doing both is simply stricter.