5.8. Setting Your Erase, Kill, and Interrupt Characters
Have you ever sat down at a terminal where the "erase" key (the character that deletes the last thing you typed) wasn't where you thought it would be? If you have, you know how disorienting this can be! On Linux, there's loadkeys. If you're using the X Window System, check into the xmodmap (Section 5.1) command. Newer shells, like bash and zsh, tend to do their own handling of these special characters -- especially during their built-in command-line editing (Section 30.14). Check your shell's manual page about readline. The most portable method is with the stty (Section 5.7) command. All of these give you a way of changing the erase character (along with several others) so you can restore some order to your world.
stty takes two kinds of input. If you want to give the command interactively, type stty erase char, where char is the key you normally use for erase -- BACKSPACE, DELETE, whatever -- followed by RETURN. This will do the trick, provided that the character you type isn't already used for something. If the character is in use or if you're putting stty commands into your .login, .profile, or .bash_profile file, it's better to "spell these characters out." "Control" characters in .login are allowed, but they aren't a great idea. If you like to use the BACKSPACE key as the erase key, add the following line:
stty erase ^h
If you want to use the DELETE key, quote the ? character so the shell won't treat it as a wildcard (Section 1.13):
stty erase ^\?
That is, stty lets you represent a control key with the two-character combination ^x, where ^ is the literal key ^ (caret) and x is any single character. You may need to put a \ before the x to prevent the shell from interpreting it as a wildcard [and a \ before the ^ to prevent old Bourne shells from interpreting it as a pipe! -- JP].
Of course, you're not limited to the BACKSPACE or DELETE keys; you can choose any other key you want. If you want to use "Z" as your DELETE key, type stty erase Z. Just make sure you never want to type a real Z!
Table 5-1 lists functions that stty can change.
Table 5-1. Keys to set with stty
The command stty everything (BSD derivatives) or stty -a (Linux and System V derivatives) shows all your current terminal settings. The werase and rprnt characters aren't implemented on some older versions of Unix, though they are on Linux and Darwin and most other modern Unix variants.
It's amazing how often you'll see even moderately experienced Unix users holding down the BACKSPACE or DELETE key to delete a partially completed command line that contains an error.
It's usually easier to use the line-kill characters -- typically CTRL-u or CTRL-x. (The command stty -a or stty everything (Section 41.3) will tell you which. Section 5.7 shows how to change them.) The line-kill character will work on a command line (at a shell prompt (Section 4.1)) and in other places where the terminal is in cooked mode. Some Unix programs that don't run in cooked mode, like vi, understand the line-kill character, too.
Even better, many stystems have a "word-erase" character, usually CTRL-2, which deletes only back to the previous whitespce. There's no need to delete the entire command line if you want to change only part of it!
As a historical note, the erase character was originally #, and the kill character was originally @. These assignments go back to the olden days, when terminals printed with real ink on real paper and made lots of noise. However, I'm told that there are some modern systems on which these settings are still the default.
NOTE: Terminal emulators, editors, and other programs can fool around with all of this stuff. They should be well behaved and reset your terminal when you leave them, but that's often not true. So don't expect your settings to work within a terminal emulator; they may, or they may not. And don't expect your settings to be correct after you exit from your terminal emulator. Again, they may, or they may not. This is primarily due to the fact that some terminal-emulator programs lie about the extent to which they support a given set of control codes.
--ML, JP, SJC, and TOR
Copyright © 2003 O'Reilly & Associates. All rights reserved.