HowToReadline

Revision 5 as of 2006-06-10 21:21:20

Clear message

Note: this article was written before release 6.06. A new version of the article is being worked on to bring it up to date.

If you use a bash shell, you might know it uses the Readline library for editing command lines. Many other programs use it too. The Readline configuration supplied with Ubuntu and Kubuntu allows you to use some obvious keys, such as the left/right arrow keys, for moving around and editing the command line, but you might want a few features beyond those provided. In fact, Readline has a rich set of default key bindings, but they can be difficult to remember or awkward to use. For instance, <Alt>f and <Alt>b move forwards and backwards a word at a time. Enter the bash builtin command bind -P for a complete list of the bindings.

This article describes how to add some keys and key combinations to the repertoire. A new Readline configuration file is attached which defines the following keys:

<Ctrl><arrow>

Move forwards and backwards a word at a time

<Ctrl><Delete>

Delete word to right of cursor

<Ctrl><Backspace>

Delete word to left of cursor

<Shift><Delete>

Delete from cursor to end of line

<Shift><Backspace>

Delete from cursor to start of line

<Insert>

Toggle overwrite/insert mode1

1 Each call to Readline starts in insert mode; the Insert key doesn't "stick".

A word is defined by Readline to be a sequence of letters or numbers; it is not possible to change this definition. Other parts of Ubuntu might define a word differently. For instance, Konsole by default defines the characters in: :@-./_~ to be part of a word when double clicking (see Settings/Configure Konsole).

Keyboard map

Before installing the Readline configuration file, it is necessary to configure the terminal emulator to emit a unique byte sequence for each required key combination. For instance, Konsole with the default XTerm (XFree 4.x.x) keyboard table emits the same byte sequence (<Esc>[OD) for <Ctrl><left arrow> as for <left arrow>.

Attached to this article is attachment:myKubuntu.keytab, which configures Konsole to emit the required byte sequences. Download it and put it in /usr/share/apps/konsole or ~/.kde/share/apps/konsole (~ is the conventional notation for "my home directory"). Then in Konsole select "Settings/Keyboard/XTerm (myKubuntu)". If you're happy with this after testing, select "Settings/Save as Default".

See /usr/share/apps/konsole/README.KeyTab for details of how the keytab file is written. (Note that /usr/include/qt3/qnamespace.h has the prefixes "Key_", not "Qt::Key_".) /usr/share/apps/konsole/README.default.Keytab lists the Konsole builtin keyboard table on which myKubuntu.keytab is based. Some of these default key bindings needed to be changed, due to the way Konsole works. For instance, the default table configures the Backspace key alone to emit "\x7f", but this means the Backspace key together with the Ctrl and/or Shift key also emits "\x7f". Since we need distinct byte sequences for <Backspace>, <Ctrl><Backspace>, and <Shift><Backspace>, the plain "Backspace" definition had to be changed to "Backspace-Shift-Control".

If you don't use the default Konsole keyboard table, and need to modify a different keytab file, a useful technique during development is to start Konsole with the command konsole --keytab myown. This allows you to see any error messages from Konsole, for instance those that say a key combination has been defined earlier in the keytab file. To verify a key combination, a useful technique is to start vim, enter Insert mode with i, and enter <Ctrl>V followed by the key combination.

[FIXME: not described are the GNOME terminal emulator, the Linux tty console, or classic xterm. The new Konsole byte sequences follow the classic xterm translations where available, so that <Ctrl><arrow> and <Insert> work in classic xterm.]

Readline configuration

After your terminal emulator is configured to emit the required byte sequences, download the file attachment:inputrc and put it in /etc or in your home directory as .inputrc.

Summary

You have modified your terminal emulator to emit the byte sequences required by your custom Readline configuration file. Now bash and other command-line programs can use the keys defined in the table above, as well as Home, End, etc.

[http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html GNU Readline Library]

[http://www.gnu.org/software/bash/manual/bashref.html Bash Reference Manual]

[http://www.white.nu/~jwhite/keymapping/ Andrew's (barebones) guide to terminals and keymappings]


CategoryDocumentation CategoryCleanup