readline - get a line from a user with editing
#include <stdio.h> #include <readline/readline.h> #include <readline/history.h>
char * readline (const char *prompt);
Readline is Copyright (C) 1989-2002 by the Free Software Foundation, Inc.
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.
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.
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.
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__. 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).
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.
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 "\e[[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
In addition to the GNU Emacs style escape sequences, a second set of backslash escapes is available:
When entering the text of a macro, single or double quotes should 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 '.
Bash allows the current readline key bindings to be displayed or modified with the bind builtin command. The editing mode may be switched during interactive use by using the -o option to the set builtin command. Other programs using this library provide similar mechanisms. The inputrc file may be edited and re-read if a program does not provide any other means to incorporate new bindings.
Readline has variables that can be used to further customize its behavior. A variable may be set in the inputrc file with a statement of the form
set variable-name value
Except where noted, readline variables can take the values On or Off (without regard to case). The variables and their default values are:
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.
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.
$if Bash # Quote the current or previous word "\C-xq": "\eb\"\ef\"" $endif
$include /etc/inputrc
Readline provides commands for searching through the command history for lines containing a specified string. There are two search modes: incremental and non-incremental.
Incremental searches begin before the user has finished typing the search string. As each character of the search string is typed, readline displays the next entry from the history matching the string typed so far. An incremental search requires only as many characters as needed to find the desired history entry. To search backward in the history for a particular string, type C-r. Typing C-s searches forward through the history. The characters present in the value of the isearch-terminators variable are used to terminate an incremental search. If that variable has not been assigned a value the Escape and C-J characters will terminate an incremental search. C-G will abort an incremental search and restore the original line. When the search is terminated, the history entry containing the search string becomes the current line.
To find other matching entries in the history list, type C-s or C-r as appropriate. This will search backward or forward in the history for the next line matching the search string typed so far. Any other key sequence bound to a readline command will terminate the search and execute that command. For instance, a newline will terminate the search and accept the line, thereby executing the command from the history list. A movement command will terminate the search, make the last line found the current line, and begin editing.
Non-incremental searches read the entire search string before starting to search for matching history lines. The search string may be typed by the user or be part of the contents of the current line.
The following is a list of the names of the commands and the default key sequences to which they are bound. Command names without an accompanying key sequence are unbound by default.
In the following descriptions, point refers to the current cursor position, and mark refers to a cursor position saved by the set-mark command. The text between the point and mark is referred to as the region.
The following is a list of the default emacs and vi bindings. Characters with the eighth bit set are written as M-<character>, and are referred to as metafied characters. The printable ASCII characters not mentioned in the list of emacs standard bindings are bound to the self-insert function, which just inserts the given character into the input line. In vi insertion mode, all characters not specifically mentioned are bound to self-insert. Characters assigned to signal generation by stty(1) or the terminal driver, such as C-Z or C-C, retain that function. Upper and lower case metafied characters are bound to the same function in the emacs mode meta keymap. The remaining characters are unbound, which causes readline to ring the bell (subject to the setting of the bell-style variable).
Emacs Standard bindings
Emacs Meta bindings
Emacs Control-X bindings
VI Insert Mode functions
; "C-[[" vi-movement-mode ; "C-_" undo
VI Command Mode functions
Brian Fox, Free Software Foundation
bfox@gnu.org
Chet Ramey, Case Western Reserve University
chet@ins.CWRU.Edu
If you find a bug in readline, you should report it. But first, you should make sure that it really is a bug, and that it appears in the latest version of the readline library that you have.
Once you have determined that a bug actually exists, mail a bug report to bug-readline@gnu.org. If you have a fix, you are welcome to mail that as well! Suggestions and `philosophical' bug reports may be mailed to bug-readline@gnu.org or posted to the Usenet newsgroup gnu.bash.bug.
Comments and bug reports concerning this manual page should be directed to chet@ins.CWRU.Edu.
It's too big and too slow.
2 pages link to readline(3):
lib/main.php:944: Notice: PageInfo: Cannot find action page
lib/main.php:839: Notice: PageInfo: Unknown action