readline (3) Linux Manual Page
readline – get a line from a user with editing
Synopsis
#include <readline/history.h>
#include <readline/readline.h>
#include <stdio.h>
char *
readline(const char *prompt);
Copyright
Readline is Copyright (C) 1989-2011 Free Software Foundation, Inc.
Description
readline will read a line from the terminal and return it, using prompt as a prompt. If prompt is NULL or the empty string, no prompt is issued. The line returned is allocated with malloc(3); the caller must free it when finished. The line returned has the final newline removed, so only the text of the line remains.
readline offers editing capabilities while the user is entering the line. By default, the line editing commands are similar to those of emacs. A vi-style line editing interface is also available.
This manual page describes only the most basic use of readline. Much more functionality is available; see The GNU Readline Library and The GNU History Library for additional information.
Return Value
readline returns the text of the line read. A blank line returns the empty string. If EOF is encountered while reading a line, and the line is empty, NULL is returned. If an EOF is read with a non-empty line, it is treated as a newline.
Notation
An Emacs-style notation is used to denote keystrokes. Control keys are denoted by C-key, e.g., C-n means Control-N. Similarly, meta keys are denoted by M-key, so M-x means Meta-X. (On keyboards without a meta key, M-x means ESC x, i.e., press the Escape key then the x key. This makes ESC the meta prefix. The combination M-C-x means ESC-Control-x, or press the Escape key then hold the Control key while pressing the x key.)
Readline commands may be given numeric arguments, which normally act as a repeat count. Sometimes, however, it is the sign of the argument that is significant. Passing a negative argument to a command that acts in the forward direction (e.g., kill-line) causes that command to act in a backward direction. Commands whose behavior with arguments deviates from this are noted.
When a command is described as killing text, the text deleted is saved for possible future retrieval (yanking). The killed text is saved in a kill ring. Consecutive kills cause the text to be accumulated into one unit, which can be yanked all at once. Commands which do not kill text separate the chunks of text on the kill ring.
Initialization File
Readline is customized by putting commands in an initialization file (the inputrc file). The name of this file is taken from the value of the INPUTRC environment variable. If that variable is unset, the default is ~/.inputrc. If that file does not exist or cannot be read, the ultimate default is /etc/inputrc. When a program which uses the readline library starts up, the init file is read, and the key bindings and variables are set. 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. Other lines denote key bindings and variable settings. Each program using this library may add its own commands and bindings.
For example, placing
- M-Control-u: universal-argument
or
- C-Meta-u: universal-argument
into the inputrc would make M-C-u execute the readline command universal-argument.
The following symbolic character names are recognized while processing key bindings: DEL, ESC, ESCAPE, LFD, NEWLINE, RET, RETURN, RUBOUT, SPACE, SPC, and TAB.
In addition to command names, readline allows keys to be bound to a string that is inserted when the key is pressed (a macro).
Key Bindings
The syntax for controlling key bindings in the inputrc file is simple. All that is required is the name of the command or the text of a macro and a key sequence to which it should be bound. The name may be specified in one of two ways: as a symbolic key name, possibly with Meta- or Control- prefixes, or as a key sequence. The name and key sequence are separated by a colon. There can be no whitespace between the name and the colon.
When using the form 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).
In the second form, "keyseq":function-name or macro, keyseq differs from keyname above in that strings denoting an entire key sequence may be specified by placing the sequence within double quotes. Some GNU Emacs style key escapes can be used, as in the following example, but the symbolic character names are not recognized.
- "\C-u": universal-argument
"\C-x\C-r": re-read-init-file
"[11~": "Function Key 1"
In this example, C-u is again bound to the function universal-argument. 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 full set of GNU Emacs style escape sequences available when specifying key sequences is
-
\C-- control prefix
\M-- meta prefix
- an escape character
\- backslash
\- literal ", a double quote
\’- literal ‘, a single quote
In addition to the GNU Emacs style escape sequences, a second set of backslash escapes is available:
-
- alert (bell)
- backspace
\d- delete
- form feed
- newline
- carriage return
- horizontal tab
- vertical tab
\nnn- the eight-bit character whose value is the octal value nnn (one to three digits)
