home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 41.8 ptys and Window Systems Chapter 41
Terminal and Serial Line Settings
Next: 41.10 Using terminfo Capabilities in Shell Programs
 

41.9 Commands to Adjust Your Terminal

Most terminals and window systems read every character that the host computer sends to them. They're watching for an escape sequence , a series of characters that give commands to the terminal or window. (From now on, I'll just say "terminal." But this article applies to windows, too.) When the terminal sees an escape sequence, it performs the command instead of showing you the characters.

You can send these escape sequences yourself, from your UNIX account. For instance, maybe some program has accidentally left your terminal set to reverse video. If you're using an ANSI terminal (like a VT100) you could type an echo command (8.6 ) to send the sequence ^[[0m (where ^[ is an ESCape character) to turn off reverse video. However, it's usually easier to make aliases, a shell function or script that does the same thing.

These escape sequences should be documented in your terminal's manual. In this article, I'll use sequences for a VT102 or compatible terminal. If you're trying to make your setup more general so that it works on lots of terminals, you should use a command like tput or tcap (41.10 ) that reads your terminal's terminfo or termcap information.

For example, you might decide that it's easier to read a complicated display when it's in reverse video. To put it in reverse video and go back to normal video later:

% Revvid


%   ...type commands; all text shows in reverse video...

% Normal


%   ...now everything is in normal video...

Most full-screen programs (vi , etc.) re-initialize your terminal. That can undo some of what these commands do.

To write the script, make a file named for one of the aliases, like Clear . The first line of the script stores an ESC character in a shell variable (6.8 ) named e . It's used in all the other aliases as ${e} :




echo..3'
 























$*
 









#! /bin/sh
# SENDS VT102 AND COMPATIBLE TERMINAL CONTROL ESCAPE SEQUENCES

e="`echo e | tr e '\033'`"  # Make an ESCape character portably

case "$0" in
*Clear) seq="${e}[;H${e}[2J"  ;;# move to top left, clear screen

# ALTERNATE CHARACTER SETS.  YOU USUALLY WANT "NOG" TO CLEAR THESE
# WHEN YOUR TERMINAL GETS IN THIS MODE ACCIDENTALLY:

*NOG) seq="${e}(B" ;;       # cancel graphics
*Graphics) seq="${e}(0" ;;  # lower-case letters become graphics

# NOTE: THESE WON'T WORK FOR FULL-SCREEN APPLICATIONS LIKE vi.
# BETTER TO RESET YOUR TERMINAL PARAMETERS (tset, stty):

*C132) seq="${e}[?3;h" ;;   # 132-column mode
*C80) seq="${e}[?3;l" ;;    # 80-column mode

*Revvid) seq="${e}[?5;h" ;; # Reverse video
*Normal) seq="${e}[?5;l" ;; # Normal video

# WRITE MESSAGE TO TERMINAL STATUS LINE (NICE FOR REMINDERS)
#       EXAMPLE: ToStatus Clean out your files!
# AND CLEAR IT.

*ToStatus) seq="${e}7${e}[25;1f${e}[0K$*${e}8" ;;
*ClrStatus) seq="${e}7${e}[25;1f${e}[0K${e}8" ;;
*) echo "$0: HELP - can't run myself." 1>&2; exit 1;;
esac

# SEND $seq TO TERMINAL WITHOUT INTERPRETATION BY SYSTEM V echo:
cat << END_OF_seq
$seq
END_OF_seq
exit 0

You can install this script from the CD-ROM or from the online archive (52.7 ) . If you don't get the file from the disc, be careful to type those escape sequences exactly. The Graphics command uses the digit 0, not the letter O. The ToStatus and ClrStatus commands use the digit 1 (one), not the letter l (L). If you get the script from the archive, ask tar to install Clear and its eight other links:

% tar xvf 

archive.tar

 Clear NOG Graphics C132 \
C80 Revvid Normal ToStatus ClrStatus


x Clear, 1371 bytes, 3 tape blocks
NOG linked to Clear
Graphics linked to Clear
   ...

The script tests the name it was called with (44.22 ) , in $0 , to decide which string to output (the asterisk (* ) matches any pathname before the command name). This trick saves disk space. You can add other commands, too, by adding a line to the case and another link.

- JP based on a suggestion and aliases by Bruce Barnett


Previous: 41.8 ptys and Window Systems UNIX Power Tools Next: 41.10 Using terminfo Capabilities in Shell Programs
41.8 ptys and Window Systems Book Index 41.10 Using terminfo Capabilities in Shell Programs

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