Linux explores 01-stty and keyboard signals

Posted by tomz0r on Tue, 25 Jan 2022 08:35:54 +0100

-----Last updated [December 20, 2021]-----

1, Introduction

The original Unix setup assumed that people used terminals to connect to host computers. More than 30 years later, this is still the case, even running Unix on your own PC. Over the years, terminals have developed into many different types and provided many different types of keyboards, but Unix has been able to use them well. This is because Unix uses a keyboard mapping system, which is very flexible and can be used for any type of keyboard.

The so-called keyboard mapping system uses a set of standard keyboard signals. Although these signals are standard, the keys to be pressed to send these signals can be modified as needed, and stty(set tty) provided by GNU is a tool to modify these keys.

Generally speaking, keyboard mapping is that when you press one or some keys, it will send a set signal to your system. For example, in Unix, characters are mapped to intr(interrupt) signals. That is, when pressed, its effect is to send an intr (terminate program) signal.

The stty command described in this section can be used to view or modify the current keyboard mapping.

< Ctrl-C > means holding down the Ctrl key of the keyboard and pressing the C letter key. Of course, there are other representations, such as ^ C or Ctrl-C.

2, stty syntax

stty - change and print terminal line settings

Usage: stty [-F DEVICE | --file=DEVICE] [SETTING]...
  or:  stty [-F DEVICE | --file=DEVICE] [-a|--all]
  or:  stty [-F DEVICE | --file=DEVICE] [-g|--save]

For more detailed usage, you can use man stty or stty --help.

3, Common key mappings

The key mappings listed below are the most common, but they are not fixed.

signal key effect
erase / / ^H Delete the last character you typed
werase ^W Delete the last word you typed
kill ^X / ^U Delete entire row
intr ^C Stop running programs
quit ^\ Stop the program and save the core file
stop ^S Pause screen display
start ^Q Restart screen display
eof ^D Indicates that there is no data (stop shell, i.e. exit login)
susp ^Z Pause a program (pending)

Because of the literal meaning, the kill keyboard signal can easily be mistaken for stopping the program, but its function is to delete the line you just typed. In order to stop the program, you need to use the intr signal.

4, stty usage

1. View key mappings

Use stty -a to view the keyboard mapping of the system

root@instance-2:~# stty -a
speed 38400 baud; rows 23; columns 110; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc

This ^? Not a real key combination. That is, you can't press and hold the < Ctrl > key again? (question mark) key. ^? Which key on your keyboard that sends the code that used to be called DEL.

The options shown here can be reconfigured as needed. Each terminal option has its own name (such as ixon), and most of them are either set or cleared. In stty, the corresponding option is preceded by a negative sign (-), and the option is cleared (prohibited); If there is no negative sign, this option is set. When you want to modify the value of each selection, you should write the corresponding option correctly in the stty command. For example:

$ stty -ixon    # Set flow control to OFF
$ stty ixon    # Set it to ON. Multiple options can be set simultaneously in the stty command: 
$ stty ixon 1200    # This command sets the terminal to 1200 baud and sets the flow control to ON.

2. Modify key mapping

To modify the key mapping, you only need to use the command stty, followed by the name of the signal to be modified, and then the new key assignment.

For example, the command to change the kill signal to ^ U is as follows:

stty kill ^U

3. Other settings

1) Echo settings

stty -echo    #Turn off echo. For example, when used to enter a password in a script.
stty echo     #Turn on echo.

2) Print the size of the current terminal (number of rows and columns)

root@Chan:~# stty size
38 191

3) Disable lowercase output on the command line

stty olcuc     #open
stty -olcuc    #recovery

4) Ignore carriage return

stty igncr    #open
stty -igncr   #recovery

5, Common keyboard mapping problems

1. Solve the backspace key mapping problem

1) In the command line interface on linux/unix platform:
When you enter the wrong character and want to delete it, and then habitually press the backspace key, you find that not only the character you want to delete is not deleted, but also two more characters ^ H.

2) Cause of problem:
On your computer, the key is equivalent to ^ H, but on the remote host, the ^ H key is not mapped to erase signal (^? Mapped to erase).

3) Solution:
Method 1:
Modify the configuration of the program connecting to the remote host (such as the ssh client program you use). Most communication programs allow the control key to send ^ H or send ^?.
Method 2:
This can be achieved by using the stty command to modify the settings of the tty terminal.

stty erase ^H

If you want to use this setting the next time you log in, you need to put this setting command into your initialization file (usually your. profile file).

2. Closed eof

Most shell s use ^ D to map eof signals, but sometimes you just accidentally press < ctrl-c >, and then log you out.
To avoid this, you can set the shell to block the eof signal.
In Bash shell, you can set the environment variable $IGNOREEOF as follows:

linux1@noseeu:~$ export IGNOREEOF=5
linux1@noseeu:~$ echo $IGNOREEOF
linux1@noseeu:~$ Use "logout" to leave the shell.
linux1@noseeu:~$ Use "logout" to leave the shell.
linux1@noseeu:~$ Use "logout" to leave the shell.
linux1@noseeu:~$ Use "logout" to leave the shell.
linux1@noseeu:~$ Use "logout" to leave the shell.
linux1@noseeu:~$ logout


At this time, you can only log off your login by pressing < ctrl-c > five times in a row.

6, Reference:

Bookcase: Chapter 7 of UNIX & Linux University Course (US): Harley Hahn, translated by Zhang Jieliang
GNU Manual: man stty
Chinese Manual:
English materials: The TTY decrypted
SegmentFault: Linux TTY/PTS overview

Topics: Linux Unix