GNU Readline

{{short description|Software library}}

{{Infobox software

| name = GNU Readline

| logo = Heckert GNU white.svg

| logo size = 64px

| author = Brian Fox

| developer = Chet Ramey

| released = {{Start date and age|1989}}

| latest release version = {{wikidata|property|preferred|references|edit|Q578173|P348|P548=Q2804309}}

| latest release date = {{wikidata|qualifier|preferred|single|Q578173|P348|P548=Q2804309|P577}}

| latest preview version = {{wikidata|property|preferred|references|edit|Q578173|P348|P548=Q51930650}}

| latest preview date = {{wikidata|qualifier|preferred|single|Q578173|P348|P548=Q51930650|P577}}

| programming language = C

| genre = Library

| license = 2009: GPL-3.0-or-later{{efn|GPL-3.0-or-later since version 6.0 (2009-02-20).}}
1997: GPL-2.0-or-later{{efn|GPL-2.0-or-later from version 2.1 (1997-06-05) until version 5.2 (2006-10-11).}}
1994: GPL-1.0-or-later{{efn|GPL-1.0-or-later from ? until version 2.0 (1994-08-04).}}

}}

GNU Readline is a software library that provides in-line editing and history capabilities for interactive programs with a command-line interface, such as Bash. It is currently maintained by Chet Ramey as part of the GNU Project.

It allows users to move the text cursor, search the command history, control a kill ring (a more flexible version of a copy/paste clipboard) and use tab completion on a text terminal. As a cross-platform library, readline allows applications on various systems to exhibit identical line-editing behavior.

Editing modes

Readline supports both Emacs and vi editing modes, which determine how keyboard input is interpreted as editor commands. See {{Section link|Editor war|Comparison}}.

=Emacs keyboard shortcuts=

Emacs editing mode key bindings are taken from the text editor Emacs.

On some systems, {{key press|Esc}} must be used instead of {{key press|Alt}}, because the {{key press|Alt}} shortcut conflicts with another shortcut. For example, pressing {{key press|Alt|f}} in Xfce's terminal emulator window does not move the cursor forward one word, but activates "File" in the menu of the terminal window, unless that is disabled in the emulator's settings.

  • {{key press|TAB}} : Autocompletes from the cursor position.
  • {{key press|Ctrl|a}} : Moves the cursor to the line start (equivalent to the key {{key press|Home}}).
  • {{key press|Ctrl|b}} : Moves the cursor back one character (equivalent to the key {{key press|Left}}).
  • {{key press|Ctrl|c}} : Sends the signal SIGINT via pseudoterminal to the current task, which aborts and closes it.{{efn|A part of pseudoterminals or PTYs (for Linux), which is evident from the terminal configuration tools such as stty; in typical Linux distribution the PTY master endpoint treats special characters as signals to be sent to slaves e.g.{{code|SIGINT}}{{Cite web|title=pty(7) - Linux manual page|url=https://man7.org/linux/man-pages/man7/pty.7.html|access-date=2021-09-30|website=man7.org}}}}
  • {{key press|Ctrl|d}}
  • Sends an EOF marker, which (unless disabled by an option) closes the current shell (equivalent to the command exit). (Only if there is no text on the current line)
  • If there is text on the current line, deletes the current character (then equivalent to the key {{key press|Delete}}).
  • {{key press|Ctrl|e}} : (end) moves the cursor to the line end (equivalent to the key {{key press|End}}).
  • {{key press|Ctrl|f}} : Moves the cursor forward one character (equivalent to the key {{key press|Right}}).
  • {{key press|Ctrl|g}} : Abort the reverse search and restore the original line.
  • {{key press|Ctrl|h}} : Deletes the previous character (same as backspace).
  • {{key press|Ctrl|i}} : Equivalent to the tab key.
  • {{key press|Ctrl|j}} : Equivalent to the enter key.
  • {{key press|Ctrl|k}} : Clears the line content after the cursor and copies it into the clipboard.
  • {{key press|Ctrl|l}} : Clears the screen content (equivalent to the command clear).
  • {{key press|Ctrl|n}} : (next) recalls the next command (equivalent to the key {{key press|Down}}).
  • {{key press|Ctrl|o}} : Executes the found command from history, and fetch the next line relative to the current line from the history for editing.
  • {{key press|Ctrl|p}} : (previous) recalls the prior command (equivalent to the key {{key press|Up}}).
  • {{key press|Ctrl|r}} : (reverse search) recalls the last command including the specified characters. A second {{key press|Ctrl|r}} recalls the next anterior command that corresponds to the search.
  • {{key press|Ctrl|s}} : Go back to the next more recent command of the reverse search (beware to not execute it from a terminal because this command also launches its XOFF). If you changed that XOFF setting, use {{key press|Ctrl|q}} to return.
  • {{key press|Ctrl|t}} : Transpose the previous two characters.
  • {{key press|Ctrl|u}} : Clears the line content before the cursor and copies it into the clipboard.
  • {{key press|Ctrl|v}} : If the next input is also a control sequence, type it literally (e. g. * {{key press|Ctrl|v}} {{key press|Ctrl|h}} types "^H", a literal backspace).
  • {{key press|Ctrl|w}} : Clears the word before the cursor and copies it into the clipboard.
  • {{key press|Ctrl|x}} {{key press|Ctrl|e}} : Edits the current line in the $EDITOR program, or vi if undefined.
  • {{key press|Ctrl|x}} {{key press|Ctrl|r}} : Read in the contents of the inputrc file, and incorporate any bindings or variable assignments found there.
  • {{key press|Ctrl|x}} {{key press|Ctrl|u}} : Incremental undo, separately remembered for each line.
  • {{key press|Ctrl|x}} {{key press|Ctrl|v}} : Display version information about the current instance of Bash.
  • {{key press|Ctrl|x}} {{key press|Ctrl|x}} : Alternates the cursor with its old position. (C-x, because x has a crossing shape).
  • {{key press|Ctrl|y}} : (yank) adds the clipboard content from the cursor position.
  • {{key press|Ctrl|z}} : Sends the signal SIGTSTP to the current task, which suspends it. To execute it in background one can enter bg. To bring it back from background or suspension fg ['process name or job id'] (foreground) can be issued.
  • {{key press|Ctrl|_}} : Incremental undo, separately remembered for each line.
  • {{key press|Alt|b}} : (backward) moves the cursor backward one word.
  • {{key press|Alt|c}} : Capitalizes the character under the cursor and moves to the end of the word.
  • {{key press|Alt|d}} : Cuts the word after the cursor.
  • {{key press|Alt|f}} : (forward) moves the cursor forward one word.
  • {{key press|Alt|l}} : Lowers the case of every character from the cursor's position to the end of the current word.
  • {{key press|Alt|r}} : Cancels the changes and puts back the line as it was in the history.
  • {{key press|Alt|u}} : Capitalizes every character from the cursor's position to the end of the current word.
  • {{key press|Alt|.}} : Insert the last argument to the previous command (the last word of the previous history entry).

Choice of the GPL as GNU Readline's license

GNU Readline is notable for being a free software library which is licensed under the GNU General Public License (GPL). Free software libraries are far more often licensed under the GNU Lesser General Public License (LGPL), for example, the GNU C Library, GNU gettext and FLTK. A developer of an application who chooses to link to an LGPLv3 licensed library can use any license that does not: "restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications".{{cite web

|url=https://www.gnu.org/copyleft/lesser.html

|title=GNU Lesser General Public License

|year=2007

|website=The GNU Lesser General Public License v3.0 - GNU Project

|publisher=Free Software Foundation

|accessdate=2011-09-03

}} But linking to a GPLv3 licensed library such as Readline requires the entire combined resulting application to be licensed under the GPLv3 when distributed, to comply with section 5 of the GPL.{{cite web

|url=https://www.gnu.org/licenses/gpl-3.0.html

|title=GNU General Public License

|year=2007

|website=The GNU General Public License v3.0 - GNU Project

|publisher=Free Software Foundation

|accessdate=2011-09-03

}}{{cite web

|url=https://www.gnu.org/licenses/gpl-faq.html#IfLibraryIsGPL

|title=Frequently Asked Questions about the GNU licenses

|year=2010

|website=Frequently Asked Questions about the GNU Licenses - GNU Project

|publisher=Free Software Foundation

|accessdate=2011-09-03

}}

This licensing was chosen by the FSF on the hopes that it would encourage software to switch to the GPL.{{cite web

|url=https://www.gnu.org/licenses/why-not-lgpl.en.html

|title=Why you shouldn't use the Lesser GPL for your next library

|year=2016

|website=Why you shouldn't use the Lesser GPL for your next library - GNU Project - Free Software Foundation

|publisher=Free Software Foundation

|accessdate=2019-10-15

}} An important example of an application changing its licensing to comply with the copyleft conditions of GNU Readline is CLISP, an implementation of Common Lisp. Originally released in 1987, it changed to the GPL license in 1992,{{cite web

|url=http://clisp.cvs.sourceforge.net/viewvc/clisp/clisp/COPYRIGHT

|title=CLISP copyright notice

|year=1992

|publisher=CLISP repository

|accessdate=2011-09-03

}} after an email exchange between one of CLISP's original authors, Bruno Haible, and Richard Stallman, in which Stallman argued{{cite web

|url=https://gitlab.com/gnu-clisp/clisp/-/raw/master/doc/Why-CLISP-is-under-GPL

|title=Why CLISP is under GPL

|year=1992

|publisher=CLISP repository

|accessdate=2023-01-19

}} that the linking of readline in CLISP meant that Haible was required to re-license CLISP under the GPL if he wished to distribute the implementation of CLISP which used readline.{{cite web

|url=http://www.clisp.org/impnotes/faq.html#faq-gpl

|title=License - why GNU GPL?

|website=Frequently Asked Questions (With Answers) about CLISP

|publisher=CLISP team

|accessdate=2011-09-03

}}

Another response has been to not use this in some projects, making text input use the primitive Unix terminal driver for editing.

= Alternative libraries =

Alternative libraries have been created with other licenses so they can be used by software projects which want to implement command line editing functionality, but be released with a non-GPL license.

  • Many BSD systems have a BSD-licensed libedit.{{Cite web |title=editline(3) - NetBSD Manual Pages |url=https://man.netbsd.org/editline.3 |access-date=2022-09-13 |website=NetBSD |quote=Command Line Editor Library (libedit, -ledit)}}[https://thrysoee.dk/editline/ Editline Library (libedit)] MariaDB and PHP allow for the user to select at build time whether to link with GNU Readline or with libedit.{{cite web|url=https://github.com/MariaDB/server/blob/10.2/BUILD/SETUP.sh|title=MariaDB/server/blob/10.2/BUILD/SETUP.sh|website=MariaDB repository|publisher=MariaDB Foundation|accessdate=2017-10-24}}{{cite web|url=https://github.com/php/php-src/tree/master/ext/readline|title=php/php-src/tree/master/ext/readline|website=PHP repository|publisher=The PHP Group|accessdate=2017-10-24}}
  • linenoise is a tiny C library that provides line editing functions.{{cite web |last1=Sanfilippo |first1=Salvatore |title=antirez/linenoise |website=GitHub |url=https://github.com/antirez/linenoise |date=10 May 2020}} {{as of|2025|post=,}} it is prominently used by MongoDB and Redis.{{cite web |url=https://github.com/mongodb/mongo/blob/master/src/mongo/shell/linenoise.cpp |title=The MongoDB Database |website=GitHub |access-date=23 March 2025}}{{cite web |url=https://github.com/redis/redis/tree/unstable/deps/linenoise |title=Redis |website=GitHub |access-date=23 March 2025}} It was integrated into Android in 2010, but has since been deprecated.{{cite web |url=https://android.googlesource.com/platform/system/core/+/7fe202f160ca1926bc0277e3c276ad7b3f9b9aeb |title=Android core |website=Google Git |date=25 May 2010}}{{cite web |url=https://android.googlesource.com/platform/system/core/+/refs/heads/main/shell_and_utilities/ |title=Android's shell and utilities |website=Google Git}}
  • Haskeline is a BSD-3-Clause licensed readline-like library for Haskell. It is mainly written for the Glasgow Haskell Compiler,{{cite web|url=https://wiki.haskell.org/Applications_and_libraries|title=Applications and libraries|website=Haskell wiki|publisher=haskell.org|accessdate=2017-10-24}} but is available to other Haskell projects which need line-editing services as well.{{cite web|url=https://github.com/judah/haskeline|title=judah/haskeline: A Haskell library for line input in command-line programs|website=Haskeline repository|author=Judah Jacobson|accessdate=2017-10-24}}
  • PSReadLine is a BSD-2-Clause licensed readline implementation written in C# for PowerShell inspired by bash and GNU Readline{{cite web|url=https://github.com/PowerShell/PSReadLine|title=PowerShell/PSReadLine: A bash inspired readline implementation for PowerShell |website=PSReadLine repository|accessdate=2023-12-20}}

Sample code

The following code is in C and must be linked against the readline library by passing a {{mono|-lreadline}} flag to the compiler:

  1. include
  2. include
  3. include
  4. include

int main()

{

// Configure readline to auto-complete paths when the tab key is hit.

rl_bind_key('\t', rl_complete);

// Enable history

using_history();

while (1) {

// Display prompt and read input

char* input = readline("prompt> ");

// Check for EOF.

if (!input)

break;

// Add input to readline history.

add_history(input);

// Do stuff...

// Free buffer that was allocated by readline

free(input);

}

return 0;

}

Bindings

Non-C programming languages that provide language bindings for readline include

  • Python's built-in {{code|readline}} module;
  • Ruby's built-in {{code|readline}} module;{{Cite web|url=https://ruby-doc.org/stdlib/libdoc/readline/rdoc/Readline.html|title = Module: Readline (Ruby 3.0.2)}}
  • Perl's third-party (CPAN) {{code|Term::ReadLine}} module, specifically {{code|Term::ReadLine::Gnu}} for GNU ReadLine.

Support for readline alternatives differ among these bindings.

Notes

{{Notelist}}

References

{{Reflist}}