zh-CN:Readline Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary link Template:Article summary heading Template:Article summary wiki Template:Article summary end
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. If you are a vi or vim user, you may want to put the following line to your
~/.inputrc to enable vi-like keybindings:
set editing-mode vi
Alternatively, to use the vi-like keybindings, you may add the following line to your
set -o vi
Usually, pressing the up arrow key will cause the last command to be shown regardless of what 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. Had history search been enabled, only past commands beginning with
ls (the current input) would've been shown, in this case
You can enable this mode by adding the lines below 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 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 Template:Keypress+Template:Keypress, 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 Template:Keypress+Template:Keypress again during this mode will display the previous line in the buffer that matches the current search term, while pressing Template:Keypress+Template:Keypress (abort) will cancel the search and restore the current input line. So in order to search through all previous
mount commands, press Template:Keypress+Template:Keypress, type 'mount' and keep pressing Template:Keypress+Template:Keypress until the desired line is found.
The forward equivalent to this mode is called forward-search-history and is bound to Template:Keypress+Template:Keypress by default. Beware that most terminals override Template:Keypress+Template:Keypress to suspend execution until Template:Keypress+Template:Keypress is entered. (This is called XON/XOFF flow control). For activating forward-search-history, either disable flow control by issuing:
$ stty -ixon
If you repeat the same command several times, they will all be appended in your history. To prevent this, add to your
To disable logging blank commands add this to your
~/.bashrc already contains
replace it with
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"
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 Template:Keypress+Template:Keypress+Template:Keypress append "| less" to the line and run it (Template:Keypress+Template:Keypress is equivalent to Template:Keypress):
"\e\C-l":"\C-e | less\C-m"
"\e\C-y":"\C-ayes | \C-m"
"\es":"\C-a su -c '\C-e'\C-m"
"\e\C-b":"\C-e > /dev/null 2>&1 &\C-m"
Tips and tricks
Disabling control echo
set echo-control-characters off