In This chapter:
You interact with the shell through your keyboard, so it’s important to know which keys have special functions for controlling your terminal:[6]
As you issue commands, you’re bound to make typing mistakes. You can correct them by backspacing over characters or words and then retyping, or you can erase the entire line to start over.
Sometimes, you’ll want to move a slow-running command into the background so you can continue working, or kill a command that starts spewing out more output than you expected. These operations are done by typing special keys.
This chapter explains how to use the stty command to find out what your key settings are, and how to change them if you don’t like them. Your terminal is a tool that you control — it shouldn’t control you.
stty displays your current terminal settings. Its options vary from system to system, but at least one of the following command lines should produce output identifying several important terminal control functions and the characters you type to perform them:
%stty -a
%stty all
%stty everything
In the output, look for something like this:
erase kill werase rprnt flush lnext susp intr quit stop eof ^? ^U ^W ^R ^O ^V ^Z ^C ^ ^S/^Q ^D
Or like this:
intr = ^c; quit = ^; erase = ^?; kill = ^u; eof = ^d; start = ^q; stop = ^s; susp = ^z; rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v;
The words erase
, kill
, etc., indicate terminal control functions. The ^
c
sequences indicate the characters that perform the functions. For example, ^u
and ^U
represent CTRL-U
, and ^?
represents the DEL
character.
There are many special keys on your terminal; the most important are those that per form the erase
, kill
, werase
, rprnt
, lnext
, stop
, start
, intr
, susp
, and eof
functions.
The erase
, kill
, werase
, rprnt
, and lnext
characters let you do simple editing of the current command line. (Some systems do not support werase
or rprnt
.) If you use tcsh, you also have access to a built-in general purpose editor, described in Chapter 7, The tcsh Command-Line Editor.
erase
To backspace over the last character, type the erase
character. Common erase
characters are CTRL-H
(also known as BACKSPACE
) or DEL
. Terminals vary in what they provide. There is often a BACKSPACE
key that produces CTRL-H
, and/or a DEL
(or DELETE
or RUBOUT
) key that produces a DEL
character. Some keyboards have only a BACKSPACE
or DEL
key, but allow you to program the key to produce the character you want.
kill
The kill
(line kill) character completely zaps the line you’re typing so you can start over. Common kill
character settings are CTRL-U
or CTRL-X
.
werase
The werase
(word erase) character erases the last word of your command line with one keystroke. When you need to erase several characters, using word erase is often faster than hitting the erase
key over and over. The werase
key is usually CTRL-W
. (If you’re using tcsh, CTRL-W
might not do word erase. Try ESC DEL
or ESC CTRL-H
instead.)
rprnt
The rprnt
(reprint) character redisplays the command line you’re typing—useful if output gets splattered into the middle of your command line (e.g., from a background process, or from line noise over a modem connection). By reprinting the command line, you can see what you had typed. The rprnt
character is usually CTRL-R
.
If you are on a System V UNIX machine, don’t be fooled by documentation that says this control function is named reprint
. The stty command actually recognizes rprnt
.
lnext
The lnext
(literal-next) character lets you type characters into the command line that would otherwise be interpreted immediately. For instance, in tcsh, TAB
triggers filename completion. To type a literal TAB
into a command, type the lnext
character first. The lnext
character is usually CTRL-V
.
The stop
, start
, intr
, susp
, and eof
characters allow you to control running processes.
stop
, start
The stop
character stops output to your terminal until you type the start
character. These characters help you control the flow of output to your terminal.[7] The stop
and start
characters are usually CTRL-S
and CTRL-Q
. Some terminals have a “scroll lock” key that alternately generates CTRL-S
and CTRL-Q
.
intr
If you issue a command that takes too long to finish, or that runs away by producing too much output, you can usually kill it by interrupting it. The usual intr
character is CTRL-C
.
If the command that you want to interrupt disables CTRL-C
to make itself uninterruptible, try suspending the command with CTRL-Z
. Then, kill it like this:
%kill %
Try this first %kill -9 %
Try this if plain kill doesn't work
The kill character is different than the kill command; the former erases your current command line, while the latter clobbers a running program.
susp
If you want to move a command into the background (e.g., if it’s taking a long time to finish), first suspend it by typing the susp
character (usually CTRL-Z
). Then, resume the command in the background like this:
% bg
eof
The eof
character signals end-of-file to the process currently reading the terminal. If this process is the shell, the shell terminates. The eof
character is usually CTRL-D
. To prevent CTRL-D
from terminating your shell, set the ignoreeof variable in your ˜/.cshrc file:
set ignoreeof
If you don’t like your terminal’s settings, use stty to change them:
%stty
function char
function
is the control function name and char
is the associated character. For example, the following command sets the interrupt character to CTRL-C
:
% stty intr ^c
You can use either ^c
or ^C
; stty understands them both to mean CTRL-C
.
To make sure your terminal is set up properly each time you log in, put the appropriate stty commands in your ˜/.login file, following any tset or reset commands that might already be in the file. Remember to log in again so that your changes take effect. (For information about your ˜/.login file, see Chapter 4, The Shell Startup Files.)
The next two sections describe how to deal with some common problems involving the erase and line kill characters. If you think your terminal acts funny even after setting it up with stty, look at another user’s ˜/.login file or take a look at the stty manual page. You might find something specific to your system that needs to be set.
On many systems, #
and @
are the default erase
and kill
characters. These choices come from the days of hardcopy terminals and are no longer useful. If your ˜/.login file doesn’t set erase
and kill
to something else, you may have a hard time typing #
and @
. Set the kill
character in your .login file as shown below; CTRL-U
is a common choice:
stty kill ^u
Everybody makes mistakes while entering commands, so it’s important to know how to erase typing errors. Usually, you would hit the BACKSPACE
or DEL
key. If you have trouble backspacing, it’s probably because the system and you don’t agree on what the erase character should be. If you see ^H
when you try to erase characters, then your terminal is sending CTRL-H
(another name for the BACKSPACE
character). Add the following to your ˜/.login file to tell the system to interpret CTRL-H
as the erase character:
stty erase ^h Set erase character to CTRL-H
If you see ^?
when you try to erase, your terminal is sending DEL
. Put the following in ˜/.login, to tell the system to interpret DEL
as the erase character:
stty erase '^?' Set erase character to DEL
Note that ^?
should be quoted as '^?'
or ^?
to turn off the special meaning that the ?
character has to the shell as a pattern matching operator.
If your terminal seems to be locked, you may have inadvertently typed CTRL-S
, which stops terminal output. To get it going again, type CTRL-Q
. Even if CTRL-S
wasn’t the problem, CTRL-Q
won’t do any harm.
It’s also possible for your terminal to become confused, even if it’s normally set up correctly. This situation can occur for several reasons:
If your terminal is left in an unusable state under these or similar circumstances, try the following remedies:
First, try CTRL-Q
, in case a stray CTRL-S
was sent to your terminal.
If that doesn’t work, type CTRL-J
reset
CTRL-J
. (If character echoing was turned off, you might not see anything as you type.) The reset command might at least get you to the point at which you can log out and then back in, to re-establish your normal working environment.
Finally, try CTRL-C
to interrupt your current job, or CTRL-Z
to suspend the job so that you can use kill to kill it.