Visible bell mini-Howto Alessandro Rubini, alessandro.rubini@linux.it v2.1, Aug 1996 This doc explains something about termcap usage and provides a pointer to a kernel patch to completely disable audible bells on demand. 1. Introduction The Linux console driver beeps the audible bell whenever a BEL char is output (ASCII code 7). Though this is a right choice for the default behaviour, many users don't like their computer to beep. This mini- Howto is meant to explain how to tell applications not to output the BEL code. Pointers to a kernel patch are provided as well. The patch is intended as a catch-all approach to avoid mangling with termcap and applications' defaults. In my opinion the best solution is a hardware one, and my own computer doesn't even carry a loudspeaker. 2. Spekearectomy Speakerectomy is by far the most brilliant solution to the audible bell problem. It is the simplest way to remove beeps, though some additional notes are worth. PC's are usually equipped with a silly switch to toggle the mainboard clock to a lower speed. The switch is completely unneeded when you work in a multitasking environment, and even its original function to slow down old dos games based on software-loop delays is no more used. Unfortunately we can't use the switch to increase processor speed, but we can turn it in a speaker enable/disable toggle. It is nice when your computer beeps at you to signal the end of a lenghty compilation, even for those who prefer a silent console. To modify the switch functionality, just detatch it from the main board and connect its wires in series with the loudspeaker. That's all to it. Owners of laptop boxes, unfortunately, don't have easy access to the lousspeaker, and neither have a spare switch to turn to a different task. The preferred solution for such users is trying to configure their software to avoid beeping. 3. Per-console Beep Configuration As of Linux 1.3.43, Martin Mares added configurability to the bell sound in console.c. You can change the duration and pitch of the console beep on a per-console basis, by writing escape sequences to the tty. You can apply your configuration in your own ~/.profile or ~/.login, to have a different beep (or no beep) associated to each console. The escape sequences work as follow: · ESC-[10;xx] chooses the bell frequency in Hertz. The value should be in the range 21-32766, otherwise the result is undefined (at least up to the 2.0.x version -- I can't foresee the future. If the `xx' argument is missing, the default value (750Hz) will apply, as in `ESC-[10]. · ESC-[11;xx] chooses the bell duration, in milli-seconds. If you specify more than 2 seconds, the default applies (125ms). Once again, if the `xx' argument is missing (ESC-[11]) the default value will be used. To print the escape sequences, you can try for example (50Hz, 1s) "echo -e "\33[10;50]\33[11;1000]"" with bash (where "-e" means `understand escape sequences'. With tcsh "echo " 33[10;50] 33[11;1000]" will have the same effect. Note that a new `setterm' command might support bell configuration of command line, as these control codes are marked as `setterm-commands'. However, no `setterm' version I know of supports these codes. If you run Linux-1.3.43 or newer, you may be satisfied with the escape sequences and avoid reading further. If you run an older kernel (I do it myself, on a small 386), or if you want the visual bell, have a good reading. 4. Basic Concepts About Termcap The file /etc/termcap is a text file which lists the terminal capabilities. Several applications use the termcap information to move the cursor in the screen and do other screen-oriented tasks. tcsh, bash, vi and all the curses-based applications use the termcapdatabase. The database represents various terminal types, and applications use the TERM environment variable to refer to the right entry in termcap. Each capability is then represented by a two-letter code associated to the character string used to get the desired effect. The separator character between different capabilities is colon (":"). As an example, the audible bell, whith code "bl", is usually represented by the string "bl=^G", which instructs the applications to use the control-G character, the ASCII BEL. In addition to the bl capability, there is a vb capability, which represents the "visible bell". vb is usually missing in the console entry in Linux' /etc/termcap. 5. Defining a visible bell You can add the entry for the vb capability in your own termcap file. Dennis Henriksen (duke@diku.dk) suggested to insert the following line in the termcap entry for console: :vb=\E7\E[?5h\E[?5l\E[?5h\E[?5l\E[?5h\E[?5l\E[?5h\E[?5l\E8:\ The trailing backslash is used to escape the newline in the database. Dennis' code does the following (his own words): · Save the cursor position (Just a safety precaution) · Change the background color several times between normal and reverse · Restore the cursor position. Some warnings about modifying termcap: · Check what your TERM variable looks like: it used to be "console", but it depends on your distribution and kernel version. Actually, it is due to change to "linux". · Check if the vb field is already there, distributors are reading docs and upgrading their software all the time. · Some applications don't use termcap but the terminfo database. They won't sense your vb entry. Terminfo is more powerful than termcap, as well as more difficult. 6. Telling applications about it This is an incomplete list of applications that can be instrued to use the vb entry for the current terminal type: · tcsh (6.04 and later): "set visiblebell". The instruction can appear in .cshrc or can be issued interactively. To reset the audible bell just "unset visiblebell". To disable any notification issue "set nobeep". · bash (any bash, as fas as I know): put "set bell-style visible" in your ~/.bashrc. Possible bell-style's are also "none" or "audible". · bash (with readline, as well as other readline based applications): put "set prefer-visible-bell" in ~/.inputrc. · nvi and elvis: put "set flash" in ~/.exrc or tell ":set flash" interactively (note the colon). To disable the visible bell use noflash in place of flash. · emacs: put "(setq visible-bell t)" in your ~/.emacs. It is disabled by "(setq visible-bell nil)". · less: use "-q" on command line to use the visual bell, use "-Q" to disable any reporting. Default options can be put in your environment variable "LESS". · screen: issue the CtrlA-CtrlG command. It works on all the virtual screens. Refer to the man page under "CUSTOMIZATION" for setting the default. · xterm: xterm can convert each bell to either a visible or audible signal. It defualt to audible, but you can use the "-vb" command line option and the "xterm*visualBell: true" resource. You can toggle visible/audible signaling on the fly with the control- mouse-1 menu. · other X applications: you can tell the X server the volume of the bell, with the "-f volume" commandline option. "volume" is between 0 and 100. Refer to X docs/experts about how to pass command line options to the server. 7. Disabling the audible bell If you want to force the visible bell on your console you can replace the "bl" entry in termcap with the same string suggested for "vb" above. This approach can unload you from the task of customizing each application. I use this option on all the machines where I can run Linux. Note that applications with hardwired bells in their source code won't be affected by this change. 8. Easier configurability If you want the ability to choose between audible and visible bell on a console basis, you can use two different terminal types for the linux console. You can name them, for example, console and console- vb. The console entry would be the original one, while the other could feature a visual bell string for the "bl" item. Thus you can change the behaviour of your bell on a console basis: · With tcsh: "setenv TERM console-vb" to get a screen flash, and "setenv TERM console" to get the audible beep. · With bash: "TERM=console-vb; export TERM" for the flash, and "TERM=console; export TERM" for the beep. Note that the termcap format allows to define a terminal-type in terms of another, so you need to insert in the database only the differences. Refer to the manpages for more information. 9. Other solutions The bad news is that not all the applications are termcap-aware. Most small programs feature 'backslash-a' characters in the C source code. Those chars become a literal ASCII BEL in the executable binary. Real application don't usually fall in this category, but be careful of C newcomers who give your their own programs; novice computer-science students are the worst of all. The only way to shut the loudspeaker for these applications is spekearectomy, or use of the escape sequences by Martin Mares. A long time ago I made a patch for me. I don't support it anymore as the escape sequences are there. When I reworked my patch against 1.3.42 to support a configurable tone, I found that 1.3.43 already had it :-)