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

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 At IU, how do I get support for Linux or Unix?

This is document adzf in the Knowledge Base.
Last modified on 2016-11-30 12:58:30.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.