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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 42.3 Why Changing TERM Sometimes Doesn't Work Chapter 42
Problems with Terminals
Next: 42.5 Checklist: Screen Size Messed Up?
 

42.4 Checklist for Resetting a Messed Up Terminal

Gremlins (like line noise on a modem, a bug in a program, a really long line of output, non-printable characters in a file you cat to your screen, etc.) can sneak into your system somewhere and mess up your terminal screen or window. The screen could have a bunch of flashing junk, the character set could turn into hieroglyphics, words could start coming out underlined or in inverse video, the line could lock up... well, there are lots of possibilities.

Here's a rough list of things to try. This might be worth reading through right now. Some things in here need to be ready before your terminal locks up.

  • If you can get to a shell prompt (% or $ ), the first command you should try is probably:

    clear
     
    tput
     
    % clear
    
       systems with termcap
    
    % tput clear
    
       systems with terminfo
    

    That will try to erase the screen and may also cancel other problems like inverse video.

  • If running clear doesn't clear up your screen completely and your terminal has a setup menu, look for a "clear screen" function and try it. (If you don't know how your terminal's setup mode works, find the manual or find an expert. Write down the steps and keep them close to your terminal.)

  • If you have a shell prompt and you're on a system using terminfo , try these commands. Don't use tput init unless tput reset doesn't fix things:

    % tput reset
    
    
    % tput init
    
    

    If you're using a termcap system, there's no command quite like those two. You can simulate them by making an alias (10.2 ) that runs the tset (5.4 ) command from your login setup files. (Why not do it now, for the next time you get into this mess?) Here's a simple alias:

    alias newterm 'set noglob; eval `tset -srQ \!*`; unset noglob'

    The tset command usually sends resetting or initialization commands to your terminal.

  • If every character you type shows up on a different line, characters don't appear as you type them, a RETURN does nothing or prints a ^M on your screen, the backspace, interrupt, and kill keys don't work, or lines

    jump down
              the screen
                         like this

    you've probably got trouble with the settings of your port (UNIX terminal device). One of the following commands can make your terminal usable. It might not be set up the way you're used to, but at least you'll be able to log out and log in again:

    % reset
    
    
    % [CTRL-j]
     reset [CTRL-j]
    
    
    
    % stty sane
    
    
    %  [CTRL-j]
     stty sane [CTRL-j]
    
    
    

    (If your terminal has a [LINEFEED] key, you can use it instead of [CTRL-j] .)

    If the system says that those commands don't exist or are an "unknown mode," you should make yourself an alias (10.2 ) , shell function (10.9 ) , or shell script (44.2 ) that executes an stty command similar to the one below. The exact parameters you use will depend on your normal UNIX setup:

    stty echo -nl -cbreak

    Call it something like sane . You may need to execute it by typing LINEFEED or CTRL-j before and after.

    If that doesn't work perfectly, here's what to do. The next time you log in and your screen works just right, typing stty everything or stty -g (see below) should help you decide exactly what parameters to use in your sane command.

  • If the system seems to treat every character you type as a separate command (and you may not be able to see the characters you type):

    % reset
    
    
    r: Command not found.
    : No previous regular expression
    : No current filename
    : No lines in the bufferq
    
    
    %

    (It actually doesn't look quite like that, but the first e started the editor named e . The s , e , and t are all read as commands by e . You have to quit e by typing its q command. Sheesh!)

    You should make a shell function or alias - or, put a symbolic link (18.4 ) or shell script in your bin directory (4.2 ) - that lets you run the command from the previous step (reset , stty sane , etc.) by typing a single character. I picked ] (right square bracket) as the name of mine. To make mine, I made a symlink in my bin :

    % ln -s /usr/ucb/reset ]
    
    

    (Your system's reset command may have a different pathname.) Now, to fix a goofed-up terminal, I just type a ] at a shell prompt (it may need a LINEFEED or CTRL-j before and after).

  • Best of all, if your system has the command stty -g , you can use it to save your favorite terminal settings in a file. Then, when your terminal is goofed up, read those settings in again from the file. Here's how. First, when your terminal is working just the way you want it, type:

    % stty -g >$HOME/.stty
    
    

    Then make your alias, shell script, or shell function named sane , ] , etc. (previously explained) that runs the command:

    % stty `cat $HOME/.stty`
    
    

    This should restore your terminal the way it was when you first ran the stty -g command.

    If your system doesn't have stty -g , you can fake it. Run the command stty everything or stty -a and look at the settings:

    % stty everything
    
    
    speed 38400 baud, 0 rows, 0 columns
    parenb -parodd cs7 -cstopb -hupcl cread -clocal -crtscts
    -ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc
    ixon -ixany -ixoff imaxbel
    isig iexten icanon -xcase echo echoe echok -echonl -noflsh -tostop
    echoctl -echoprt echoke
    opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel -tabs
    erase  kill  werase rprnt  flush  lnext   susp   intr   quit  stop   eof
    ^?     ^U     ^W     ^R     ^O     ^V     ^Z/^Y  ^C     ^\   ^S/^Q  ^D

    Then check your stty manual page and read about those settings (some of them, like the parity settings, might not be appropriate for all your login sessions). Put the settings in your sane or ] command:

    stty icanon echo erase '^?' kill '^u' ...

    Note that if you use several different terminals, each may have different settings. Make yourself several sane commands; you might select one automatically as you log in (2.12 ) .

If worse comes to worst, try the steps from article 42.2 . Find another place to log in to your account. Run ps to find your processes on the hung-up terminal or window and kill them. Then turn off the terminal or close the window and log in again. (If you didn't have a way to kill the processes before you logged in again, be sure to kill your old processes right away after you log in.)

- JP


Previous: 42.3 Why Changing TERM Sometimes Doesn't Work UNIX Power Tools Next: 42.5 Checklist: Screen Size Messed Up?
42.3 Why Changing TERM Sometimes Doesn't Work Book Index 42.5 Checklist: Screen Size Messed Up?

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