UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 5.7 What termcap and terminfo Do and Don't Control Chapter 5
Setting Up Your Terminal
Next: 5.9 Setting Your Erase, Kill, and Interrupt Characters
 

5.8 Terminal Escape Sequences

Most terminals use special character strings called escape sequences to control their operation. These strings begin with the escape character (ASCII character 033) (51.3).

This character can be generated alone by the ESC key found on most keyboards, or by typing the left bracket character while holding down the CONTROL key (often shown as ^[). But it's also generated by many of the special keys on your keyboard. For example, an UP ARROW key might generate an escape sequence like ^[OA. When the terminal sees this sequence of characters, it knows to move the cursor up one line.

The special escape sequences used by your terminal are stored in the terminal's termcap or terminfo entry (41.11, 5.2), which allows programs to respond appropriately to all of the special keys on the keyboard. Programs themselves issue escape sequences to do such things as move around the screen, highlight text, and so on.

However, there are cases where it's useful to issue escape sequences manually - or in an alias or shell script that you write. For example, you can highlight your prompt (7.8) or write an alias to switch your terminal display to inverse video (41.9).

Most of our examples use escape sequences for the common DEC VT100 series of terminals (which are also recognized by almost all terminal emulation programs).

How do you find out what escape sequences your terminal uses? After all, it is quite hardware-specific. If you have a terminal manual, they should be listed there. Otherwise, you can look at the termcap or terminfo listing itself (5.10), and with the help of the manual page, or a book such as O'Reilly & Associates' termcap & terminfo, decipher the obscure language used there. Or, use a program like tcap or tput (41.10); it will find those sequences for you.

To actually type an escape sequence into a file, use your editor's "quote next character command" (CTRL-v in vi (31.6)) before pressing the ESC key. To use an escape character in an alias, try the technique shown in article 41.9.

Don't be confused if you see an escape sequence that looks like this:

^[[1m

Some terminals use a real left bracket at the start of their escape sequence; it will follow the escape character itself (represented as ^[). Even though they look the same on the screen, they are really different characters (CTRL-[ or ESC is different from [, just like CTRL-c is different from C).

- TOR


Previous: 5.7 What termcap and terminfo Do and Don't Control UNIX Power ToolsNext: 5.9 Setting Your Erase, Kill, and Interrupt Characters
5.7 What termcap and terminfo Do and Don't Control Book Index5.9 Setting Your Erase, Kill, and Interrupt Characters

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System