This question already has an answer here:
- stdlib and colored output in C 7 answers
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?
Use ANSI escape sequences. This article goes into some detail about them. You can use them with
printf
as well.This is a little C program that illustrates how you could use color codes:
You probably want ANSI color codes. Most *nix terminals support them.
Here's another way to do it. Some people will prefer this as the code is a bit cleaner (there are no
%s
and aRESET
color to end the coloration).This way, it's easy to do something like: