Readline is a library by the GNU Project, used by Bash and other CLI-interface programs to edit and interact with the command line. Before reading this page please refer to the library home page as only subtle configuration will be introduced here.
By default Readline uses Emacs style shortcuts for interacting with command line. However, vi style editing interface is also supported. Either way, rich sets of shortcut keys are provided for editing without using the far-away cursor keys.
set editing-mode vi
Alternatively, you can set it only for Bash by adding the following line to
set -o vi
Fast word movement
Xterm supports moving between words with
Ctrl+Right by default. To achieve this effect with other terminal emulators, find the correct terminal codes, and bind them to
For example, for urxvt:
"\e[1;5D": backward-word "\e[1;5C": forward-word
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:
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
You can enable the history search mode by adding the following lines to
If you are using vi mode, you can add the following lines to
~/.inputrc (from this post):
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 this:
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 suspend execution 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:
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
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:
set show-all-if-ambiguous on
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:
# Color files by types 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
Readline also supports binding keys to keyboard macros. For simple example, run this command in Bash:
bind '"\ew":"\C-e # macro"'
or add the part within single quotes to inputrc:
"\ew":"\C-e # macro"
Now type a line and press
W. Readline will act as though
Ctrl+E (end-of-line) had been pressed, appended with '
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
"\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 '', 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's safer because it won't input the
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"
Disabling control echo
Due to an update to
Ctrl+C is pressed. For users who wish to disable this, simply add the following to
set echo-control-characters off