The readline
library is a powerful tool that makes it easier to read and edit user input in command-line programs.
Originally developed for the GNU Project, it offers a number of features that improve the user experience when interacting with terminal applications.
readline
is used in several popular command-line tools, including the Bash shell, the Python REPL and many others.
readline
library for?The main purpose of the readline
library is to improve the text input experience in terminals. Its features include:
Line editing: Allows users to edit input lines using familiar key combinations, such as moving the cursor left/right, deleting characters, etc.
Command history: readline
keeps a history of entered commands, allowing users to navigate and reuse previous commands.
Autocompletion: Supports autocompletion of commands and arguments, increasing efficiency and reducing errors.
Editing and formatting: Allows for prompt customization, text coloring, and other formatting options.
To install the readline
library you can use your operating system’s package manager. Example on Ubuntu using APT:
You can also compile and install readline
directly, click the button below, unzip and follow the instructions inside the unzipped folder to compile and install:
Click here to download readline
readline
LibraryLet’s explore some practical examples to illustrate how the readline
library can be used.
A basic example of how to use readline
to read a line of user input.
In this example, the
readline
function is used to read a line of user input with the prompt “Type something: “. The input is then printed to the terminal.
Compile using the
-lreadline
flag, e.g.gcc main.c -lreadline
.
Compile using the
-lreadline
flag, e.g.g++ main.cpp -lreadline
.
readline
also allows you to store and reuse previous commands. See a simple example:
In this example, the command entered by the user is added to the history if it is not empty, allowing it to be reused simply by pressing the up arrow key: ↑
, if the add_history(input)
had not been added, when pressing the key, the event would display literally: ^[[A^
.
For more information, visit: https://tiswww.case.edu/php/chet/readline/rltop.html