ARCHIVED: In Unix, how can I produce color, reverse video, and other effects?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

In Unix, you can add color and emphasis to documents and programs using ANSI escape sequences. An escape sequence is the combination of an escape code (ASCII 27) followed by a left bracket ([), followed by formatting codes, and finally the letter m. When displayed by a communications program that supports ANSI terminal emulation, the escape sequence will not display, but will instead send a command to the terminal.

You can use any text editor to add escape sequences. To use Emacs, at the Unix prompt, enter:

  emacs filename

Replace filename with the name of the file you want to edit. To add color or emphasis to a text selection, encapsulate the selection with escape sequences. To create an escape sequence in Emacs, press C-q ESC , then the rest of the escape sequence. Emacs displays the ESC code as the string ^[, so, for example, the code for bold text would look like:

  ^[[1m

To turn off the formatting, at the end of the selection, create an escape sequence with a 0 for the formatting code. So, the escape sequence would look like:

  ^[[0m

The available codes are:

0
Normal text, foreground and background
1 Bold text
4 Underline
5 Blink
7 Inverse
30 Black foreground
31 Red foreground
32 Green foreground
33 Yellow foreground
34 Blue foreground
35 Magenta foreground
36 Cyan foreground
37 White foreground
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Magenta background
46 Cyan background
47 White background

You can combine codes in a single escape sequence by separating them with semicolons. For example, to create a section of text that is bold text with a yellow foreground and blue background, the escape sequence would be:

  ^[[1;33;44m

Therefore, in Emacs, the keystrokes to make the text "Hello, world" bold, with a yellow foreground and blue background, would be:

  <C-q><ESC>[1;33;44mHello, world<C-q><ESC>[0m

Once you have finished editing your file, you can view the result by entering, at the Unix prompt:

  cat filename

Again, replace filename with the name of the file. You must use the cat command to display the file because paging programs like less and more do not properly display escape sequences.

Not all communications packages understand these escape sequences. Only ANSI terminals or programs that support ANSI terminal emulation can display a file's formatting. Also, not all applications emulate the ANSI terminal in the same way, and some may do so incompletely, so test any ANSI terminal emulation software for compatibility.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document adzf in the Knowledge Base.
Last modified on 2018-01-18 12:05:00.