29.3. Readline Init File

Although the Readline library comes with a set of Emacs-like keybindings installed by default, it is possible to use a different set of keybindings. Any user can customize programs that use Readline by putting commands in an inputrc file, conventionally in his home directory. The name of this file is taken from the value of the environment variable INPUTRC. If that variable is unset, the default is ~/.inputrc.

When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.

In addition, the C-x C-r command re-reads this init file, thus incorporating any changes that you might have made to it.

29.3.1. Readline Init File Syntax

There are only a few basic constructs allowed in the Readline init file. Blank lines are ignored. Lines beginning with a # are comments. Lines beginning with a $ indicate conditional constructs (refer to Section 29.3.2 Conditional Init Constructs). Other lines denote variable settings and key bindings.

Variable Settings

You can modify the run-time behavior of Readline by altering the values of variables in Readline using the set command within the init file. The syntax is simple:

set variable value

Here, for example, is how to change from the default Emacs-like key binding to use vi line editing commands:

set editing-mode vi

Variable names and values, where appropriate, are recognized without regard to case.

A great deal of run-time behavior is changeable with the following variables.

bell-style

Controls what happens when Readline wants to ring the terminal bell. If set to none, Readline never rings the bell. If set to visible, Readline uses a visible bell if one is available. If set to audible (the default), Readline attempts to ring the terminal's bell.

comment-begin

The string to insert at the beginning of the line when the insert-comment command is executed. The default value is "#".

completion-ignore-case

If set to on, Readline performs filename matching and completion in a case-insensitive fashion. The default value is off.

completion-query-items

The number of possible completions that determines when the user is asked whether he wants to see the list of possibilities. If the number of possible completions is greater than this value, Readline will ask the user whether or not he wishes to view them; otherwise, they are simply listed. This variable must be set to an integer value greater than or equal to 0. The default limit is 100.

convert-meta

If set to on, Readline will convert characters with the eighth bit set to an ascii key sequence by stripping the eighth bit and prefixing an [ESC] character, converting them to a meta-prefixed key sequence. The default value is on.

disable-completion

If set to On, Readline will inhibit word completion. Completion characters will be inserted into the line as if they had been mapped to self-insert. The default is off.

editing-mode

The editing-mode variable controls which default set of key bindings is used. By default, Readline starts up in Emacs editing mode, where the keystrokes are most similar to Emacs. This variable can be set to either emacs or vi.

enable-keypad

When set to on, Readline will try to enable the application keypad when it is called. Some systems need this to enable the arrow keys. The default is off.

expand-tilde

If set to on, tilde expansion is performed when Readline attempts word completion. The default is off.

If set to on, the history code attempts to place point at the same location on each history line retrived with previous-history or next-history.

horizontal-scroll-mode

This variable can be set to either on or off. Setting it to on means that the text of the lines being edited will scroll horizontally on a single screen line when they are longer than the width of the screen, instead of wrapping onto a new screen line. By default, this variable is set to off.

input-meta

If set to on, Readline will enable eight-bit input (it will not clear the eighth bit in the characters it reads), regardless of what the terminal claims it can support. The default value is off. The name meta-flag is a synonym for this variable.

isearch-terminators

The string of characters that should terminate an incremental search without subsequently executing the character as a command (refer to Section 29.2.5 Searching for Commands in the History). If this variable has not been given a value, the characters [ESC] and C-J will terminate an incremental search.

keymap

Sets Readline's idea of the current keymap for key binding commands. Acceptable keymap names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command, and vi-insert. vi is equivalent to vi-command; emacs is equivalent to emacs-standard. The default value is emacs. The value of the editing-mode variable also affects the default keymap.

mark-directories

If set to on, completed directory names have a slash appended. The default is on.

mark-modified-lines

This variable, when set to on, causes Readline to display an asterisk (*) at the start of history lines which have been modified. This variable is off by default.

mark-symlinked-directories

If set to on, completed names which are symbolic links to directories have a slash appended (subject to the value of mark-directories). The default is off.

match-hidden-files

This variable, when set to on, causes Readline to match files whose names begin with a . (hidden files) when performing filename completion, unless the leading . is supplied by the user in the filename to be completed. This variable is on by default.

output-meta

If set to on, Readline will display characters with the eighth bit set directly rather than as a meta-prefixed escape sequence. The default is off.

page-completions

If set to on, Readline uses an internal more-like pager to display a screenful of possible completions at a time. This variable is on by default.

print-completions-horizontally

If set to on, Readline will display completions with matches sorted horizontally in alphabetical order, rather than down the screen. The default is off.

show-all-if-ambiguous

This alters the default behavior of the completion functions. If set to on, words which have more than one possible completion cause the matches to be listed immediately instead of ringing the bell. The default value is off.

visible-stats

If set to on, a character denoting a file's type is appended to the filename when listing possible completions. The default is off.

Key Bindings

The syntax for controlling key bindings in the init file is simple. First you need to find the name of the command that you want to change. The following sections contain tables of the command name, the default keybinding, if any, and a short description of what the command does.

Once you know the name of the command, simply place on a line in the init file the name of the key you wish to bind the command to, a colon, and then the name of the command. The name of the key can be expressed in different ways, depending on what you find most comfortable.

In addition to command names, readline allows keys to be bound to a string that is inserted when the key is pressed (a macro).

keyname: function-name or macro

keyname is the name of a key spelled out in English. For example:
Control-u: universal-argument
Meta-Rubout: backward-kill-word
Control-o: "> output"

In the above example, C-u is bound to the function universal-argument, M-DEL is bound to the function backward-kill-word, and C-o is bound to run the macro expressed on the right hand side (that is, to insert the text > output into the line).

A number of symbolic character names are recognized while processing this key binding syntax: DEL, ESC, ESCAPE, LFD, NEWLINE, RET, RETURN, RUBOUT, SPACE, SPC, and TAB.

"keyseq": function-name or macro

keyseq differs from keyname above in that strings denoting an entire key sequence can be specified, by placing the key sequence in double quotes. Some gnu Emacs style key escapes can be used, as in the following example, but the special character names are not recognized.

"\C-u": universal-argument
"\C-x\C-r": re-read-init-file
"\e[11~": "Function Key 1"

In the above example, C-u is again bound to the function universal-argument (just as it was in the first example), C-x C-r is bound to the function re-read-init-file, and [ESC] [[] [1] [1] [~] is bound to insert the text Function Key 1.

The following gnu Emacs style escape sequences are available when specifying key sequences:

\C-

control prefix

\M-

meta prefix

\e

an escape character

\\

backslash

\"

["], a double quotation mark

\'

['], a single quote or apostrophe

In addition to the gnu Emacs style escape sequences, a second set of backslash escapes is available:

\a

alert (bell)

\b

backspace

\d

delete

\f

form feed

\n

newline

\r

carriage return

\t

horizontal tab

\v

vertical tab

\nnn

the eight-bit character whose value is the octal value nnn (one to three digits)

\xHH

the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)

When entering the text of a macro, single or double quotes must be used to indicate a macro definition. Unquoted text is assumed to be a function name. In the macro body, the backslash escapes described above are expanded. Backslash will quote any other character in the macro text, including " and '. For example, the following binding will make C-x \ insert a single \ into the line:
"\C-x\\": "\\"

29.3.2. Conditional Init Constructs

Readline implements a facility similar in spirit to the conditional compilation features of the C preprocessor which allows key bindings and variable settings to be performed as the result of tests. There are four parser directives used.

$if

The $if construct allows bindings to be made based on the editing mode, the terminal being used, or the application using Readline. The text of the test extends to the end of the line; no characters are required to isolate it.

mode

The mode= form of the $if directive is used to test whether Readline is in emacs or vi mode. This may be used in conjunction with the set keymap command, for instance, to set bindings in the emacs-standard and emacs-ctlx keymaps only if Readline is starting out in emacs mode.

term

The term= form may be used to include terminal-specific key bindings, perhaps to bind the key sequences output by the terminal's function keys. The word on the right side of the = is tested against both the full name of the terminal and the portion of the terminal name before the first -. This allows sun to match both sun and sun-cmd, for instance.

application

The application construct is used to include application-specific settings. Each program using the Readline library sets the application name, and you can test for a particular value. This could be used to bind key sequences to functions useful for a specific program. For instance, the following command adds a key sequence that quotes the current or previous word in Bash:
$if Bash
# Quote the current or previous word
"\C-xq": "\eb\"\ef\""
$endif

$endif

This command, as seen in the previous example, terminates an $if command.

$else

Commands in this branch of the $if directive are executed if the test fails.

$include

This directive takes a single filename as an argument and reads commands and bindings from that file. For example, the following directive reads from /etc/inputrc:
$include /etc/inputrc

29.3.3. Sample Init File

Here is an example of an inputrc file. This illustrates key binding, variable assignment, and conditional syntax.

# This file controls the behaviour of line input editing for
# programs that use the GNU Readline library.  Existing
# programs include FTP, Bash, and GDB.
#
# You can re-read the inputrc file with C-x C-r.
# Lines beginning with '#' are comments.
#
# First, include any systemwide bindings and variable
# assignments from /etc/Inputrc
$include /etc/Inputrc

#
# Set various bindings for emacs mode.

set editing-mode emacs

$if mode=emacs

Meta-Control-h:	backward-kill-word	Text after the function name is ignored

#
# Arrow keys in keypad mode
#
#"\M-OD":        backward-char
#"\M-OC":        forward-char
#"\M-OA":        previous-history
#"\M-OB":        next-history
#
# Arrow keys in ANSI mode
#
"\M-[D":        backward-char
"\M-[C":        forward-char
"\M-[A":        previous-history
"\M-[B":        next-history
#
# Arrow keys in 8 bit keypad mode
#
#"\M-\C-OD":       backward-char
#"\M-\C-OC":       forward-char
#"\M-\C-OA":       previous-history
#"\M-\C-OB":       next-history
#
# Arrow keys in 8 bit ANSI mode
#
#"\M-\C-[D":       backward-char
#"\M-\C-[C":       forward-char
#"\M-\C-[A":       previous-history
#"\M-\C-[B":       next-history

C-q: quoted-insert

$endif

# An old-style binding.  This happens to be the default.
TAB: complete

# Macros that are convenient for shell interaction
$if Bash
# edit the path
"\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f"
# prepare to type a quoted word --
# insert open and close double quotes
# and move to just after the open quote
"\C-x\"": "\"\"\C-b"
# insert a backslash (testing backslash escapes
# in sequences and macros)
"\C-x\\": "\\"
# Quote the current or previous word
"\C-xq": "\eb\"\ef\""
# Add a binding to refresh the line, which is unbound
"\C-xr": redraw-current-line
# Edit variable on current line.
"\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y="
$endif

# use a visible bell if one is available
set bell-style visible

# don't strip characters to 7 bits when reading
set input-meta on

# allow iso-latin1 characters to be inserted rather
# than converted to prefix-meta sequences
set convert-meta off

# display characters with the eighth bit set directly
# rather than as meta-prefixed characters
set output-meta on

# if there are more than 150 possible completions for
# a word, ask the user if he wants to see all of them
set completion-query-items 150

# For FTP
$if Ftp
"\C-xg": "get \M-?"
"\C-xt": "put \M-?"
"\M-.": yank-last-arg
$endif