Readline - ArchWiki

Jump to content

# Readline

From ArchWiki

Readline is a library by the GNU Project, used by Bash and other CLI programs to edit and interact with the command line. See readline(3) for more information.

## Installation

The readline package is most likely already installed as a dependency of Bash.

## Editing mode

By default Readline uses Emacs style shortcuts for interacting with command line. However, vi style editing interface is also supported by adding the following to `~/.inputrc`:

    ~/.inputrc

    set editing-mode vi

Alternatively, to set it only for Bash by adding the following line to `~/.bashrc`:

    ~/.bashrc

    set -o vi

### Mode indicator in prompt

Vi-style editing has two modes: command and insert. You can display which one is currently active by adding the following option:

    ~/.inputrc

    set show-mode-in-prompt on

This will print a string in your prompt (`(cmd)`/`(ins)` by default) that can be customized with the `vi-ins-mode-string` and `vi-cmd-mode-string` variables.

### Different cursor shapes for each mode

You can set a different cursor shape for each mode by using "\1 .. \2" escapes:

    ~/.inputrc

    set vi-ins-mode-string \1\e[6 q\2
    set vi-cmd-mode-string \1\e[2 q\2

This will set a block shaped cursor when in command mode and a pipe cursor when in insert mode. Note that you must have the mode indicator enabled for this to work (see #Mode indicator in prompt).

The Virtual Console uses different escape codes, so you should check first which term is being used:

    ~/.inputrc

    $if term=linux
    	set vi-ins-mode-string \1\e[?0c\2
    	set vi-cmd-mode-string \1\e[?8c\2
    $else
    	set vi-ins-mode-string \1\e[6 q\2
    	set vi-cmd-mode-string \1\e[2 q\2
    $endif

See software cursor for VGA for further details.

## Fast word movement

Xterm supports moving between words with `Ctrl+Left` and `Ctrl+Right` by default. To achieve this effect with other terminal emulators, find the correct terminal codes, and bind them to `backward-word` and `forward-word` in `~/.inputrc`.

For example, for urxvt:

    ~/.inputrc

    "\e[1;5D": backward-word
    "\e[1;5C": forward-word

Another example for macOS style (`Alt+Left` and `Alt+Right`) word moving:

    ~/.inputrc or /etc/inputrc

    "\e[1;9D": backward-word
    "\e[1;9C": forward-word

## History

The history configuration is split into:

- controlling its operation, which is configured shell-specific, for example see Bash#History customization and
- influencing the interaction, which may be configured via a shell-specific configuration, or its native `inputrc` configuration applicable for all shells, as explained in the following examples.

Usually, pressing the up arrow key will cause the last command to be shown regardless of the command that has been typed so far. However, users may find it more practical to list only past commands that match the current input.

For example, if the user has typed the following commands:

- `ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig`
- `who`
- `mount`
- `man mount`

In this situation, when typing `ls` and pressing the up arrow key, current input will be replaced with `man mount`, the last performed command. If the history search has been enabled, only past commands beginning with `ls` (the current input) will be shown, in this case `ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig`.

You can enable the history search mode by adding the following lines to `/etc/inputrc` or `~/.inputrc`:

    "\e[A": history-search-backward
    "\e[B": history-search-forward

If you are using vi mode, you can add the following lines to the readline configuration file (from BBS#54972):

    ~/.inputrc

    set editing-mode vi
    $if mode=vi
    set keymap vi-command
    # these are for vi-command mode
    "\e[A": history-search-backward
    "\e[B": history-search-forward
    j: history-search-forward
    k: history-search-backward
    set keymap vi-insert
    # these are for vi-insert mode
    "\e[A": history-search-backward
    "\e[B": history-search-forward
    $endif

If you chose to add these lines to `~/.inputrc`, it is recommended that you also add the following line at the beginning of this file to avoid strange things like BBS#112537:

    $include /etc/inputrc

Alternatively, one can use reverse-search-history (incremental search) by pressing `Ctrl+r`, which does not search based on previous input but instead jumps backwards in the history buffer as commands are typed in a search term. Pressing `Ctrl+r` again during this mode will display the previous line in the buffer that matches the current search term, while pressing `Ctrl+g` (abort) will cancel the search and restore the current input line. So in order to search through all previous `mount` commands, press `Ctrl+r`, type 'mount' and keep pressing `Ctrl+r` until the desired line is found.

The forward equivalent to this mode is called forward-search-history and is bound to `Ctrl+s` by default. Beware that most terminals override `Ctrl+s` to pause, seemingly freeze, the output until `Ctrl+q` is entered. (This is called XON/XOFF flow control). For activating forward-search-history, either disable flow control by issuing:

    $ stty -ixon

or use a different key in `inputrc`. For example, to use `Alt+s` which is not bound by default:

    "\es": forward-search-history

## Faster completion

When performing tab completion, a single tab attempts to partially complete the current word. If no partial completions are possible, a double tab shows all possible completions.

The double tab can be changed to a single tab by setting:

    ~/.inputrc

    set show-all-if-unmodified on

Or you can set it such that a single tab will perform both steps: partially complete the word and show all possible completions if it is still ambiguous:

    ~/.inputrc

    set show-all-if-ambiguous on

## Colorized completion

You can enable coloring of completion of filenames with the `colored-stats` option. You can also color the identical prefix of completion-lists with `colored-completion-prefix`. For example:

    ~/.inputrc

    # Color files by types
    # Note that this may cause completion text blink in some terminals (e.g. xterm).
    set colored-stats On
    # Append char to indicate type
    set visible-stats On
    # Mark symlinked directories
    set mark-symlinked-directories On
    # Color the common prefix
    set colored-completion-prefix On
    # Color the common prefix in menu-complete
    set menu-complete-display-prefix On

## Macros

Readline also supports binding keys to keyboard macros, for example:

    $ bind '"\ew": "\C-e # macro"'

To keep it permanently, add the part within single quotes to readline's configuration file:

    ~/.inputrc

    "\ew": "\C-e # macro"

Now type a line and press `Alt+w`. Readline will act as though `Ctrl+e` (end-of-line) had been pressed, appended with '`# macro`'.

Use any of the existing keybindings within a readline macro, which can be quite useful to automate frequently used idioms. For example, this one makes `Ctrl+Alt+l` append `| less` to the line and run it (`Ctrl+m` is equivalent to `Enter`):

    "\e\C-l": "\C-e | less\C-m"

The next one prefixes the line with `yes |` when pressing `Ctrl+Alt+y`, confirming any yes/no question the command might ask:

    "\e\C-y": "\C-ayes | \C-m"

This example wraps the line in `su -c ''` and runs it, if `Alt+s` is pressed:

    "\es": "\C-a su -c '\C-e'\C-m"

This example prefixes the line with `sudo` , if `Alt+s` is pressed. It lets you review the result and will not input the `Enter` key.

    "\es": "\C-asudo \C-e"

As a last example, quickly send a command in the background with `Ctrl+Alt+b`, discarding all of its output:

    "\e\C-b": "\C-e > /dev/null 2>&1 &\C-m"

Similar to history vi-command macros can also be introduced to vi-command for values without a corresponding command or other macro purposes.

    $if mode=vi
    #vi mode settings
       set keymap vi-command
       # char search has a vi command so f could be defined
       "f": vi-char-search

       # char forward(next) and back(prev) does not have a vi-* command
       # so it can be accessed with a macro
       "zn": ";"
       "zp": ","

    # end vi mode settingss
    $endif

## Disabling control echo

Readline causes the terminal to echo `^C` after `Ctrl+c` is pressed. For users who wish to disable this, simply add the following to `~/.inputrc`:

    set echo-control-characters off

## Bracketed paste

By default, bracketed paste mode is on. It can be set manually in `~/.inputrc`:

    set enable-bracketed-paste on

This ensures that pasting into Readline inserts the clipboard as single string of characters, instead of inserting characters as if they were entered from the keyboard. This is a safety measure to prevent Readline from automatically modifying and running pasted commands.

## See also

- vi readline editing cheat sheet
- emacs readline editing cheat sheet

Categories:

- Command-line
- GNU

Readline
Add topic