Color text in terminal applications in UNIX

157,651

Solution 1

This is a little C program that illustrates how you could use color codes:

#include <stdio.h>

#define KNRM  "\x1B[0m"
#define KRED  "\x1B[31m"
#define KGRN  "\x1B[32m"
#define KYEL  "\x1B[33m"
#define KBLU  "\x1B[34m"
#define KMAG  "\x1B[35m"
#define KCYN  "\x1B[36m"
#define KWHT  "\x1B[37m"

int main()
{
    printf("%sred\n", KRED);
    printf("%sgreen\n", KGRN);
    printf("%syellow\n", KYEL);
    printf("%sblue\n", KBLU);
    printf("%smagenta\n", KMAG);
    printf("%scyan\n", KCYN);
    printf("%swhite\n", KWHT);
    printf("%snormal\n", KNRM);

    return 0;
}

Solution 2

Different solution that I find more elegant

Here's another way to do it. Some people will prefer this as the code is a bit cleaner. There are no %s and a RESET color to end the coloration.

#include <stdio.h>

#define RED   "\x1B[31m"
#define GRN   "\x1B[32m"
#define YEL   "\x1B[33m"
#define BLU   "\x1B[34m"
#define MAG   "\x1B[35m"
#define CYN   "\x1B[36m"
#define WHT   "\x1B[37m"
#define RESET "\x1B[0m"

int main() {
  printf(RED "red\n"     RESET);
  printf(GRN "green\n"   RESET);
  printf(YEL "yellow\n"  RESET);
  printf(BLU "blue\n"    RESET);
  printf(MAG "magenta\n" RESET);
  printf(CYN "cyan\n"    RESET);
  printf(WHT "white\n"   RESET);

  return 0;
}

This program gives the following output:

enter image description here


Simple example with multiple colors

This way, it's easy to do something like:

printf("This is " RED "red" RESET " and this is " BLU "blue" RESET "\n");

This line produces the following output:

execution's output

Solution 3

You probably want ANSI color codes. Most *nix terminals support them.

Solution 4

Use ANSI escape sequences. This article goes into some detail about them. You can use them with printf as well.

Share:
157,651

Related videos on Youtube

Nisanio
Author by

Nisanio

meh

Updated on July 08, 2022

Comments

  • Nisanio
    Nisanio almost 2 years

    I started to write a terminal text editor, something like the first text editors for UNIX, such as vi. My only goal is to have a good time, but I want to be able to show text in color, so I can have syntax highlighting for editing source code.

    How can I achieve this? Is there some special POSIX API for this, or do I have to use ncurses? (I'd rather not)

    Any advice? Maybe some textbooks on the UNIX API?

    • Mojtaba Hosseini
      Mojtaba Hosseini over 3 years
      You can use this simple method instead. It has some advantages over just printing colors too.
  • Admin
    Admin about 13 years
    printf(KMAG "magenta\n"); is much cleaner and faster than using %s.
  • Schroeder
    Schroeder about 11 years
    This sets the default color forever after to this new text color. To set it back to the original employ KNRM.
  • Michael Dorst
    Michael Dorst almost 11 years
    Is it possible to use a specific color (perhaps with RGB values like 880000 for dark red, etc.), or are we stuck with the 8 colors in the above example?
  • karlphillip
    karlphillip almost 11 years
    There are color codes for darker colors, bold, and other effects I dont remember. But you cant specify RGB values.
  • mf_
    mf_ over 10 years
    @Schroeder #define RESET "\033[0m", and then printf(KMAG "magenta RESET \n");
  • Ziyaddin Sadigov
    Ziyaddin Sadigov about 8 years
    KNRM is the same with RESET, isn't it?
  • David Guyon
    David Guyon about 8 years
    That's actually a good question. I guess you're right but I can't explain why. I mean, is it the same exact same code in a different format or different codes that have an identical behavior?
  • Ziyaddin Sadigov
    Ziyaddin Sadigov about 8 years
    Yes, according to your example, they are different little bit in format (\x1B and \033) but their behavior is the same.
  • David Guyon
    David Guyon about 8 years
    I found the explaination: the decimal ASCII code 27 is the escape character. The octal version of 27 is 33 (\033) and its hexadecimal version is 1B (\x1B) (ref: wiki.bash-hackers.org/scripting/terminalcodes). So yes, there are identical. Well done for finding this mistake and thanks for the feedback. I'll fix it right now ;).
  • Florian Castellane
    Florian Castellane over 7 years
    Better yet, puts( KMAG "magenta" RESET ) ;
  • mah
    mah over 6 years
    @mf_ what you typed probably is not what you meant... what you typed won't work because things within quotes aren't handled by the preprocessor.
  • trinity420
    trinity420 almost 5 years
    This syntax is new to me "printf("Name: " NAME);". It does only work with constants?
  • David Guyon
    David Guyon over 4 years
    #define NAME "John Doe" is a macro that defines a constant NAME with value "John Doe", but it is not a variable. At compilation time, all occurrences of NAME are replaced by its value. Then printf("Name: " NAME); is interpreted as printf("Name: " "John Doe");. If you want to print a variable (constant or not), you need to specify how the value should be interpreted (e.g. for decimal you specify %d). I hope this will make things a bit more clear.
  • alsaleem
    alsaleem over 3 years
    if it happens to separate the KMxx from RESET on two printf, printing "\n" in between will not reset the colors.