C-Code ändert die Textfarbe des Terminals; Wie kann ich die Standardeinstellungen wiederherstellen? Linux

Lesezeit: 2 Minuten

Benutzer-Avatar
jaspis77

Ich habe eine C-Datei, die unter Linux ausgeführt wird. Es druckt einige Zeilen in Rot (Fehler) und einige in Grün (bestanden). Wie Sie vielleicht erwarten, verwendet es Escape-Codes in der Druckf Aussagen wie folgt:

#define SCHWARZ "\033[22;30m"
#define GREEN  "\033[22;31m"

printf(GREEN "this will show up green" BLACK "\n");

If the BLACK at the end wasn’t there, the terminal text will continue to be green for everything. In case you didn’t catch it, that’s fine for a terminal window with a non-black background, but otherwise you’ll end up with black-on-black. Not good! Running the program has this problem, as does capturing the output in a text file and then viewing the file with “more” or “less”.

Is there a code to restore defaults instead of specifying a color at the end of the printf statement? This needs to be in C, but I would be interested in reading about other approaches.


I updated my macros as follows (note 31 is for red):

#define RESET_COLOR "\e[m"
#define MAKE_GREEN "\e[32m"

printf(MAKE_GREEN "this will show up green" RESET_COLOR "\n");

I found the following links helpful in understanding how these codes work:

  • The first link is (effectively) broken (the domain was not renewed). The second link is broken (times out).

    – Peter Mortensen

    Jul 10, 2021 at 16:43

user avatar
Frxstrem

Try using:

#define RESETCOLOR "\033[0m"

That should reset it to the defaults.

More about these terminal codes can be found in ANSI escape code.

user avatar
adamk

"\033[0m"

See ANSI escape code.

user avatar
Nils

Type reset in the terminal.

There is a binary found in Linux and OS X called reset.

  • Ok not the exact answer, but maybe looking at it helps.

    – Nils

    Aug 17, 2010 at 20:16

  • “reset” wipes out the terminal window and places the prompt at the top of the screen, which is not the effect I need, but thank you.

    – jasper77

    Aug 18, 2010 at 15:11

1206510cookie-checkC-Code ändert die Textfarbe des Terminals; Wie kann ich die Standardeinstellungen wiederherstellen? Linux

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy