Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

ANSI colors in the terminal

Let's see how to use ANSI colors in the terminal. I tried it under Linux only. Let's do it manually, without any library.

const
  bold  = "\e[1m"
  blue  = "\e[34m"
  reset = "\e[0m"

echo bold & "Hello, Bold!" & reset                    # v1

echo bold & blue & "Hello, Bold and Blue!" & reset    # v2

𝥶It prints the text in bold (v1). In v2, bold and blue are combined.

You can use a small helper function:

const
  bold  = "\e[1m"
  reset = "\e[0m"

proc boldText(s: string): string =
  bold & s & reset

echo boldText("Hello!") & " back to normal"

Demo

The "ac" prefix stands for "ANSI color".

const
  # Reset
  acReset     = "\e[0m"

  # Styles
  acBold      = "\e[1m"
  acDim       = "\e[2m"
  acItalic    = "\e[3m"
  acUnderline = "\e[4m"
  acBlink     = "\e[5m"
  acReverse   = "\e[7m"
  acHidden    = "\e[8m"
  acStrike    = "\e[9m"

  # Foreground colors
  acBlack   = "\e[30m"
  acRed     = "\e[31m"
  acGreen   = "\e[32m"
  acYellow  = "\e[33m"
  acBlue    = "\e[34m"
  acMagenta = "\e[35m"
  acCyan    = "\e[36m"
  acWhite   = "\e[37m"

  # Bright foreground colors
  acBrightBlack   = "\e[90m"
  acBrightRed     = "\e[91m"
  acBrightGreen   = "\e[92m"
  acBrightYellow  = "\e[93m"
  acBrightBlue    = "\e[94m"
  acBrightMagenta = "\e[95m"
  acBrightCyan    = "\e[96m"
  acBrightWhite   = "\e[97m"

  # Background colors
  acBgBlack   = "\e[40m"
  acBgRed     = "\e[41m"
  acBgGreen   = "\e[42m"
  acBgYellow  = "\e[43m"
  acBgBlue    = "\e[44m"
  acBgMagenta = "\e[45m"
  acBgCyan    = "\e[46m"
  acBgWhite   = "\e[47m"

  # Bright background colors
  acBgBrightBlack   = "\e[100m"
  acBgBrightRed     = "\e[101m"
  acBgBrightGreen   = "\e[102m"
  acBgBrightYellow  = "\e[103m"
  acBgBrightBlue    = "\e[104m"
  acBgBrightMagenta = "\e[105m"
  acBgBrightCyan    = "\e[106m"
  acBgBrightWhite   = "\e[107m"

when isMainModule:
  # Styles
  echo acBold      & "acBold"      & acReset
  echo acDim       & "acDim"       & acReset
  echo acItalic    & "acItalic"    & acReset
  echo acUnderline & "acUnderline" & acReset
  echo acBlink     & "acBlink"     & acReset
  echo acReverse   & "acReverse"   & acReset
  echo acStrike    & "acStrike"    & acReset

  # Foreground colors
  echo acBlack   & "acBlack"   & acReset
  echo acRed     & "acRed"     & acReset
  echo acGreen   & "acGreen"   & acReset
  echo acYellow  & "acYellow"  & acReset
  echo acBlue    & "acBlue"    & acReset
  echo acMagenta & "acMagenta" & acReset
  echo acCyan    & "acCyan"    & acReset
  echo acWhite   & "acWhite"   & acReset

  # Bright foreground colors
  echo acBrightBlack   & "acBrightBlack"   & acReset
  echo acBrightRed     & "acBrightRed"     & acReset
  echo acBrightGreen   & "acBrightGreen"   & acReset
  echo acBrightYellow  & "acBrightYellow"  & acReset
  echo acBrightBlue    & "acBrightBlue"    & acReset
  echo acBrightMagenta & "acBrightMagenta" & acReset
  echo acBrightCyan    & "acBrightCyan"    & acReset
  echo acBrightWhite   & "acBrightWhite"   & acReset

  # Background colors
  echo acBgBlack   & "acBgBlack"   & acReset
  echo acBgRed     & "acBgRed"     & acReset
  echo acBgGreen   & "acBgGreen"   & acReset
  echo acBgYellow  & "acBgYellow"  & acReset
  echo acBgBlue    & "acBgBlue"    & acReset
  echo acBgMagenta & "acBgMagenta" & acReset
  echo acBgCyan    & "acBgCyan"    & acReset
  echo acBgWhite   & "acBgWhite"   & acReset

  # Bright background colors
  echo acBgBrightBlack   & "acBgBrightBlack"   & acReset
  echo acBgBrightRed     & "acBgBrightRed"     & acReset
  echo acBgBrightGreen   & "acBgBrightGreen"   & acReset
  echo acBgBrightYellow  & "acBgBrightYellow"  & acReset
  echo acBgBrightBlue    & "acBgBrightBlue"    & acReset
  echo acBgBrightMagenta & "acBgBrightMagenta" & acReset
  echo acBgBrightCyan    & "acBgBrightCyan"    & acReset
  echo acBgBrightWhite   & "acBgBrightWhite"   & acReset

  # Combinations
  echo acBold & acRed              & "acBold & acRed"              & acReset
  echo acBold & acGreen            & "acBold & acGreen"            & acReset
  echo acBold & acBrightYellow     & "acBold & acBrightYellow"     & acReset
  echo acItalic & acCyan           & "acItalic & acCyan"           & acReset
  echo acUnderline & acMagenta     & "acUnderline & acMagenta"     & acReset
  echo acBold & acBgBlue & acWhite & "acBold & acBgBlue & acWhite" & acReset

Screenshot:

Same thing in C

In C, just use "\033" instead of "\e". The ASCII code of the ESC key is 27, which is 0o33 in octal, and 0x1b in hexa.

Everything else is the same:

#include <stdio.h>

#define BOLD "\033[1m"
#define BLUE "\033[34m"
#define RESET "\033[0m"

int main()
{
    printf("%sHello, Bold!%s\n", BOLD, RESET);

    printf("%s%sHello, Bold and Blue!%s\n", BOLD, BLUE, RESET);

    return 0;
}

Same thing in Python

#!/usr/bin/env python3

RED = "\033[31m"
GREEN = "\033[32m"
BLUE = "\033[34m"
BOLD = "\033[1m"
RESET = "\033[0m"


def main():
    print(f"{BOLD}Hello, Bold!{RESET}")
    print(f"{BOLD}{BLUE}Hello, Bold and Blue!{RESET}")
    print(f"{BOLD}{GREEN}Hello, Bold and Green!{RESET}")
    print(f"{BOLD}{RED}Hello, Bold and Red!{RESET}")


##############################################################################

if __name__ == "__main__":
    main()
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 May 12, 20:57