Day 37: Learning ANSI Escape Codes

As a reward for myself I focused on learning ANSI escape code syntax for styling text, background, and moving the cursor around. I am still a noob but it’s been pretty fun playing around and configuring my shell.

TLDR;

Okay, so here are the highlights of what I did:

  • I started reading through a great blog post on ANSI escape codes.
  • I started writing my custom shell config file in my home directory. I ended up breaking the program right away after my first changes but then I worked kind of.

Notes from ANSI Escape Code Blog Post


ANSI Text Colors:

The most basic terminals have a set of 8 different colors:

  • Black: \u001b[30m
  • Red: \u001b[31m
  • Green: \u001b[32m
  • Yellow: \u001b[33m
  • Blue: \u001b[34m
  • Magenta: \u001b[35m
  • Cyan: \u001b[36m
  • White: \u001b[37m
  • Reset: \u001b[0m

Most terminals, apart from the basic set of 8 colors, also support the “bright” or “bold” colors. These have their own set of codes, mirroring the normal colors, but with an additional ;1 in their codes:

  • Bright Black: \u001b[30;1m
  • Bright Red: \u001b[31;1m
  • Bright Green: \u001b[32;1m
  • Bright Yellow: \u001b[33;1m
  • Bright Blue: \u001b[34;1m
  • Bright Magenta: \u001b[35;1m
  • Bright Cyan: \u001b[36;1m
  • Bright White: \u001b[37;1m

Lastly, after the 16 colors, some terminals support a 256-color extended color set. These are of the form \u001b[38;5;${ID}m . Here are some examples:

  • white: \u001b[38;5;255m
  • red: \u001b[38;5;196m

ANSI Background Colors:

The Ansi escape codes let you set the color of the text-background the same way it lets you set the color of the foregrond. For example, the 8 background colors correspond to the codes:

  • Bg Black: \u001b[40m
  • Bg Red: \u001b[41m
  • Bg Green: \u001b[42m
  • Bg Yellow: \u001b[43m
  • Bg Blue: \u001b[44m
  • Bg Magenta: \u001b[45m
  • Bg Cyan: \u001b[46m
  • Bg White: \u001b[47m

With the bright version being:

  • Bg Bright Black: \u001b[40;1m
  • Bg Bright Red: \u001b[41;1m
  • Bg Bright Green: \u001b[42;1m
  • Bg Bright Yellow: \u001b[43;1m
  • Bg Bright Blue: \u001b[44;1m
  • Bg Bright Magenta: \u001b[45;1m
  • Bg Bright Cyan: \u001b[46;1m
  • Bg Bright White: \u001b[47;1m

The bright versions of the background colors do not change the background, but rather make the foreground text brighter. (Weird right? lol)

The reset remains the same as \u001b[0m

The 256 version of background colors is the same except that the code used is with 4.. and not 3.. So the code is \u001b[48;5;${ID}m . For example:

  • Bg Black: \u001b[38;5;0m
  • Bg blue: \u001b[38;5;20m

Decorations

Apart from colors, and background-colors, Ansi escape codes also allow decorations on the text:

  • Bold: \u001b[1m
  • Underline: \u001b[4m
  • Reversed: \u001b[7m
# Which can be used individually:
print u"\u001b[1m BOLD \u001b[0m\u001b[4m Underline \u001b[0m\u001b[7m Reversed \u001b[0m"

# or can be use together:
print u"\u001b[1m\u001b[4m\u001b[7m BOLD Underline Reversed \u001b[0m"

Conclusion

That’s all for today. If you are interested in the MIT course you can check out the video lecture I’m currently going through. The lecture is helpful but isn’t sufficient by itself. Anyways, until next time PEACE!