Consistent Keyboard Configuration

Eliminate inconsistent behavior from your keyboard by following the instructions in this article.
Bash

Create a new file, /etc/inputrc for system-wide use or ~/.inputrc for personal use. This will be a readline startup file (see bash(1)).

First, make the DELETE key delete characters under the cursor, and make HOME and END work by adding:

DEL: delete-char
# Home.
"\\e[1~": beginning-of-line
# End.
"\\e[4~": end-of-line"

DEL is a special symbol bash understands to be DELETE. For the HOME and END keys, their escape sequences are given in quotation marks, followed by a colon, and then the command.

To determine the escape sequence transmitted by a key, look it up in custom.map. Alternately, bring up Emacs, then type CTRL-Q followed by the key. That will usually insert the escape sequence into the buffer. The ESCAPE character will appear as ^[, i.e., control-left bracket.

The readline commands shown thus far are taken from the bash man page. If you would like to program a key to enter a regular command when pressed, put the command in quotation marks. For example, to make the 1 key on the numeric keypad list the current directory, add these lines:

# KP_1
"\\eOq": "ls\\C-m"

The \\C-m is a CTRL-M, or a carriage return, which, as we know, is necessary to execute the command.

Other keys may be programmed as desired. Save the file.

If a system-wide /etc/inputrc file was created, add the following line to /etc/profile:

export INPUTRC=/etc/inputrc

Then type:

# . /etc/profile
This will override the default initialization file, ~/.inputrc.

Regardless of where the file was created, it may now be activated by typing CTRL-XCTRL-R, which is normally bound to the readline command “re-read-init-file.”

Less

Create a new file, /etc/lesskey, for system-wide use. This will be a lesskey(1) input file. Type these lines exactly as shown and then save the file:

#line-edit
\\177    delete
^H      backspace
\\e[1~   home
\\e[4~   end

The control line, #line-edit, introduces bindings for line-editing commands. \\177 is the octal representation of DELETE, and ^H represents CTRL-H, which is a BACKSPACE. The escape sequences are those transmitted by the HOME and END keys.

Compile the file as follows:

# lesskey -o /etc/less /etc/lesskey

Add the following line to /etc/profile:

export LESS="-k/etc/less$"
Then type:
# . /etc/profile
The next time less is executed, it will read the key definitions from /etc/less. When entering a command line at the bottom of the screen (for example, the pattern for a search command), these key definitions will be active.

If a user wants to add personal key definitions to less, they may be placed in a file, say ~/lesskey, then compiled as follows:

# lesskey -o ~/.less ~/lesskey

These personal key definitions will be activated in addition to those defined system-wide.

Netscape and Minicom

Since Netscape is from the DOS/Windows world, it requires no special configuration. The designers made BACKSPACE, DELETE, HOME, END, PAGE UP and PAGE DOWN work just as expected. It simply shows that somebody did a good job, and it lends credibility to this design.

If minicom is used, then as root, enter the command:

# minicom -s

This will activate the configuration menu for minicom. Select “Screen and keyboard” from the menu. Type “A,” then press the space bar. Use “B” to determine what BACKSPACE should transmit. Since most remote systems will use DELETE to delete to the left, “DEL” is probably the best choice here. If this creates a problem, change it to BS. Press ESC when done.

Back at the configuration menu, select “Save setup as dfl,” then “Exit from Minicom.”

Emacs

Emacs can be executed in at least three different environments: in a virtual console, in an xterm, or in an X11 window manager. To run Emacs within an xterm, the -nw option must be used. Regardless of the environment, the keys will be made to perform identical functions.

Emacs contains a series of many keymaps that translate input events into other input events and eventually into commands. Three of these keymaps are diagrammed in Figure 1. The ellipses indicate the omission of keymaps beyond the scope of this article.

As root, change directory to /usr/lib/emacs/XX.XX/lisp/term, where XX.XX is the version of Emacs installed. This directory contains initialization files for several types of terminals. Read the README file for more information.

Make a backup of xterm.el, then edit this file to look like Listing 1 This file maps the escape sequences sent by the keys onto Emacs vectors of length 1. A vector is a general-purpose Emacs array, and vector values are enclosed in brackets, e.g.,.

When Emacs is executed in an xterm or a virtual console, it reads this file. The function-key-map is then configured such that when any of these keys are pressed, Emacs automatically translates the escape sequences into vectors. This makes it easier to configure Emacs, not only because all of the keys can be referred to by their names, but also because the programmer never needs to remember the escape sequences after this file has been created. Furthermore, these vectors have the same names as the X Window keysyms, thereby providing consistency among all three execution environments.

The PAGE UP/DOWN keys are now referred to as prior and next. These definitions provide support for some of the Emacs commands, such as calendar, which expect these keys to be available. Also, even though /etc/X11/Xmodmap defines these keys to transmit the Page_Up and Page_Down keysyms, Emacs sees them as prior and next when running under X.

Two sets of definitions are listed for the arrow keys, Reset or Normal and Set or Application. This is because the VT100 series of terminals has two modes of operation in which keys transmit different escape sequences. Notice that in /usr/lib/kbd/keytables/custom.map, ALT-UP ARROW 17) transmits \\e\\e[A, but in /etc/X11/Xresources, ALT-UP ARROW transmits \\e\\eOA. In both cases, these are an ESCAPE followed by the escape sequence transmitted by the UP ARROW key. One method of dealing with this problem is simply to define both sets of escape sequences, as is shown here.

The configuration file, xterm.el, needs be compiled for increased performance, as well as to overwrite the previously compiled version. After saving the file, enter the following command:

# emacs -batch -f batch-byte-compile xterm.el

Since this file is also designed for the virtual consoles, make links as follows:

# ln -s xterm.el con80x25.el
# ln -s xterm.elc con80x25.elc
trl
The .elc files, which are compiled, are the ones used by Emacs.

For running Emacs directly under X, some changes need to be made to x-win.el. Please make a backup copy, then find and change the following lines as shown:

(define-key function-key-map [backspace] [?\\b])
(define-key function-key-map [M-backspace] [?\\M-\\b])
(put 'backspace 'ascii-character 8)

Save the file. Compile it with:

# emacs -batch -f batch-byte-compile x-win.el
Emacs does not need to know about escape sequences when running directly under an X11 window manager, because it receives the keysyms directly and converts them into vectors automatically. However, since the design states that the BACKSPACE key transmits a BACKSPACE, not a DELETE, three lines had to be changed to make x-win.el comply. Note that M-backspace is short for Meta-backspace, which is a synonym for ALT-backspace.

Now it is finally time for the last step, assigning functions to the Emacs keys. Change the directory to /usr/lib/emacs/site-lisp, which is where local Emacs Lisp programs are stored.

Create a file named “ibmkey.el” to define the key bindings for an IBM PC-compatible keyboard. Enter as much of Listing 2, ibmkey.el, as desired.

The default Emacs key-translation-map is defined in /usr/lib/emacs/XX.XX/lisp/loaddefs.el. This file is included in the emacs-el (GNU Emacs LISP files) package of the Debian distribution. It normally maps the F1 key and the HELP key (if present) to BACKSPACE, because BACKSPACE (CTRL-H) is normally mapped to the Emacs help-command in the global-map. Refer to Figure 1.

However, to accommodate the design presented here, the key-translation-map will be redefined. Since Emacs is based on the assumption that DELETE is to be used to delete characters to the left of the cursor, and since the design presented here assumes that the BACKSPACE key will perform that function, the key-translation-map will be used to switch the BACKSPACE and the DELETE keys. After this is done, pressing the BACKSPACE key will cause a DELETE to come out of the key-translation-map, and pressing DELETE will cause a BACKSPACE to come out.

To reduce confusion, the variable “BACKSPACE” is defined to hold a DELETE, and the variable “DELETE” is defined to hold a BACKSPACE. This way, when BACKSPACE is pressed, the value of the variable “BACKSPACE” will come out of the key-translation-map, and when DELETE is pressed, the value of the variable “DELETE” will come out, thus eliminating the confusion caused by the switch.

Many function keys are defined as examples, using the global-map. Since BACKSPACE is no longer available for help, F1 is now mapped directly to that function, without the use of the key-translation-map. F4 is mapped to Undo. The BACKSPACE key and the editing keys are defined to perform their labeled functions, except for prior and next, which are already defined by Emacs (see loaddefs.el). While holding down ALT, the arrow keys will pan the text in all four directions. Finally, the keys of the numeric keypad are defined to perform various functions.

There are several sources of information for programming Emacs. Press F1 twice to access the extensive online help facility. Also, the GNU Emacs Lisp Reference Manual (800+ pages) is available at ftp://prep.ai.mit.edu/pub/gnu/elisp-manual-19-2.4.tar.gz for the truly serious Emacs Lisp programmer. This manual can also be ordered from the Free Software Foundation. The order forms are located in /usr/lib/emacs/XX.XX/etc/ORDERS*.

Now save, and then compile ibmkey.el:

# emacs -batch -f batch-byte-compile ibmkey.el

Finally, edit /usr/lib/emacs/site-lisp/site-start.el, and add this line:

(load "ibmkey")
This file will automatically load ibmkey.el (or ibmkey.elc, if ibmkey.el was compiled) when Emacs is started by anyone on the system. If personal key mappings are desired, place them in ~/.emacs.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Ctrl-m in emacs

softexp23's picture

How can I bind Ctrl-m to some command, in emacs, without binding return to that command. So how can I separate Ctrl-m from return ? And not using X. Cause I use emacs over ssh.

Thanks

Re: Consistent Keyboard Configuration

jago25_98's picture

Nice.

However, what do you do if the loadkeys command isn't present?

Ditto for the find command.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix