Difference between revisions of "Readline"

From ArchWiki
Jump to: navigation, search
(7 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Command shells (English)]]  
+
[[Category:Command shells]]  
{{i18n|Readline}}
+
[[it:Readline]]
 +
[[zh-CN:Readline]]
 
{{Article summary start}}
 
{{Article summary start}}
 
{{Article summary text|Improving readline over its default capabilities.}}
 
{{Article summary text|Improving readline over its default capabilities.}}
 +
{{Article summary heading|Important pages}}
 +
{{Article summary link|Official site|http://www.gnu.org/s/readline/}}
 
{{Article summary heading|Related}}
 
{{Article summary heading|Related}}
 
{{Article summary wiki|Bash}}
 
{{Article summary wiki|Bash}}
Line 9: Line 12:
 
'''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 [http://www.gnu.org/s/readline/ page] as only subtle configuration will be introduced here.  
 
'''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 [http://www.gnu.org/s/readline/ page] as only subtle configuration will be introduced here.  
  
=Command-line editing=
+
== Command-line editing ==
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 {{Filename|~/.inputrc}} to enable vi-like keybindings:
+
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 {{Ic|~/.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 {{Ic|~/.bashrc}}:
 
  set -o vi
 
  set -o vi
  
 
You may find either [http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf vi] or [http://www.catonmat.net/download/readline-emacs-editing-mode-cheat-sheet.pdf emacs] cheat sheets useful.
 
You may find either [http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf vi] or [http://www.catonmat.net/download/readline-emacs-editing-mode-cheat-sheet.pdf emacs] cheat sheets useful.
  
=History=
+
== History ==
 
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.
 
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:
 
For example, if the user has typed the following commands:
* {{Codeline|ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig}}
+
* {{Ic|ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig}}
* {{Codeline|who}}
+
* {{Ic|who}}
* {{Codeline|mount}}
+
* {{Ic|mount}}
* {{Codeline|man mount}}
+
* {{Ic|man mount}}
In this situation, when typing {{codeline|ls}} and pressing the up arrow key, current input will be replaced with {{codeline|man mount}}, the last performed command. Had history search been enabled, only past commands beginning with {{codeline|ls}} (the current input) would've been shown, in this case {{codeline|ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig}}.
+
In this situation, when typing {{Ic|ls}} and pressing the up arrow key, current input will be replaced with {{Ic|man mount}}, the last performed command. Had history search been enabled, only past commands beginning with {{Ic|ls}} (the current input) would've been shown, in this case {{Ic|ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig}}.
  
You can enable this mode by adding the lines below to {{Filename|/etc/inputrc}} or {{Filename|~/.inputrc}}:
+
You can enable this mode by adding the lines below to {{Ic|/etc/inputrc}} or {{Ic|~/.inputrc}}:
 
  "\e[A":history-search-backward
 
  "\e[A":history-search-backward
 
  "\e[B":history-search-forward
 
  "\e[B":history-search-forward
  
If you chose to add the aforementioned two lines to {{Filename|~/.inputrc}}, it is recommended that you also add the following line at the beginning of this file to avoid strange things like [https://bbs.archlinux.org/viewtopic.php?id=112537 this]:
+
If you are using vi mode, you can add the following lines to {{Ic|~/.inputrc}} (from [https://bbs.archlinux.org/viewtopic.php?pid=428760#p428760 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 {{Ic|~/.inputrc}}, it is recommended that you also add the following line at the beginning of this file to avoid strange things like [https://bbs.archlinux.org/viewtopic.php?id=112537 this]:
  
 
  $include /etc/inputrc
 
  $include /etc/inputrc
  
Alternatively, one can use reverse-search-history (incremental search) by pressing {{Keypress|Ctrl}}+{{Keypress|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 {{Keypress|Ctrl}}+{{Keypress|R}} again during this mode will display the previous line in the buffer that matches the current search term, while pressing {{Keypress|Ctrl}}+{{Keypress|G}} (abort) will cancel the search and restore the current input line. So in order to search through all previous {{codeline|mount}} commands, press {{Keypress|Ctrl}}+{{Keypress|R}}, type 'mount' and keep pressing {{Keypress|Ctrl}}+{{Keypress|R}} until the desired line is found.
+
Alternatively, one can use reverse-search-history (incremental search) by pressing {{Keypress|Ctrl}}+{{Keypress|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 {{Keypress|Ctrl}}+{{Keypress|R}} again during this mode will display the previous line in the buffer that matches the current search term, while pressing {{Keypress|Ctrl}}+{{Keypress|G}} (abort) will cancel the search and restore the current input line. So in order to search through all previous {{Ic|mount}} commands, press {{Keypress|Ctrl}}+{{Keypress|R}}, type 'mount' and keep pressing {{Keypress|Ctrl}}+{{Keypress|R}} until the desired line is found.
  
 
The forward equivalent to this mode is called forward-search-history and is bound to {{Keypress|Ctrl}}+{{Keypress|S}} by default. Beware that most terminals override {{Keypress|Ctrl}}+{{Keypress|S}} to suspend execution until {{Keypress|Ctrl}}+{{Keypress|Q}} is entered. (This is called XON/XOFF flow control). For activating forward-search-history, either disable flow control by issuing:
 
The forward equivalent to this mode is called forward-search-history and is bound to {{Keypress|Ctrl}}+{{Keypress|S}} by default. Beware that most terminals override {{Keypress|Ctrl}}+{{Keypress|S}} to suspend execution until {{Keypress|Ctrl}}+{{Keypress|Q}} is entered. (This is called XON/XOFF flow control). For activating forward-search-history, either disable flow control by issuing:
 
  $ stty -ixon
 
  $ stty -ixon
or use a different key in {{Filename|inputrc}}. For example, to use {{Keypress|Alt}}+{{Keypress|S}} which is not bound by default:
+
or use a different key in {{Ic|inputrc}}. For example, to use {{Keypress|Alt}}+{{Keypress|S}} which is not bound by default:
 
  "\es":forward-search-history
 
  "\es":forward-search-history
  
==History search==
+
===History search===
=== Avoid duplicates ===
+
==== Avoid duplicates ====
If you repeat the same command several times, they will all be appended in your history. To prevent this, add to your {{Filename|~/.bashrc}}:
+
If you repeat the same command several times, they will all be appended in your history. To prevent this, add to your {{Ic|~/.bashrc}}:
 
  export HISTCONTROL=ignoredups
 
  export HISTCONTROL=ignoredups
  
=== Avoid whitespaces ===
+
==== Avoid whitespaces ====
To disable logging blank commands add this to your {{Filename|~/.bashrc}}:
+
To disable logging blank commands add this to your {{Ic|~/.bashrc}}:
 
  export HISTCONTROL=ignorespace
 
  export HISTCONTROL=ignorespace
If your {{Filename|~/.bashrc}} already contains  
+
If your {{Ic|~/.bashrc}} already contains  
 
  export HISTCONTROL=ignoredups
 
  export HISTCONTROL=ignoredups
 
replace it with
 
replace it with
 
  export HISTCONTROL=ignoreboth
 
  export HISTCONTROL=ignoreboth
  
=Macros=
+
== Macros ==
 
Readline also supports binding keys to keyboard macros. For simple example, run this command in Bash:
 
Readline also supports binding keys to keyboard macros. For simple example, run this command in Bash:
 
  bind '"\ew":"\C-e # macro"'
 
  bind '"\ew":"\C-e # macro"'
Line 60: Line 80:
 
  "\ew":"\C-e # macro"
 
  "\ew":"\C-e # macro"
  
Now type a line and press {{Keypress|Alt}}+{{Keypress|W}}. Readline will act as though {{Keypress|Ctrl}}+{{Keypress|E}} (end-of-line) had been pressed, appended with '{{codeline| # macro}}'.
+
Now type a line and press {{Keypress|Alt}}+{{Keypress|W}}. Readline will act as though {{Keypress|Ctrl}}+{{Keypress|E}} (end-of-line) had been pressed, appended with '{{Ic| # 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 {{Keypress|Ctrl}}+{{Keypress|Alt}}+{{Keypress|L}} append "| less" to the line and run it ({{Keypress|Ctrl}}+{{Keypress|M}} is equivalent to {{Keypress|Enter}}:
+
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 {{Keypress|Ctrl}}+{{Keypress|Alt}}+{{Keypress|L}} append "| less" to the line and run it ({{Keypress|Ctrl}}+{{Keypress|M}} is equivalent to {{Keypress|Enter}}):
 
  "\e\C-l":"\C-e | less\C-m"
 
  "\e\C-l":"\C-e | less\C-m"
  
Line 68: Line 88:
 
  "\e\C-y":"\C-ayes | \C-m"
 
  "\e\C-y":"\C-ayes | \C-m"
  
This example wraps the line in {{Codeline|su -c ''}}, if {{Keypress|Alt}}+{{Keypress|S}} is pressed:
+
This example wraps the line in {{Ic|su -c ''}}, if {{Keypress|Alt}}+{{Keypress|S}} is pressed:
 
  "\es":"\C-a su -c '\C-e'\C-m"
 
  "\es":"\C-a su -c '\C-e'\C-m"
  
Line 74: Line 94:
 
  "\e\C-b":"\C-e > /dev/null 2>&1 &\C-m"
 
  "\e\C-b":"\C-e > /dev/null 2>&1 &\C-m"
  
=Tips and Tricks=
+
== Tips and tricks ==
==Disabling control echo==
+
=== Disabling control echo ===
Due to an update to {{Package Official|readline}}, the terminal now echoes {{Codeline|^C}} after {{Keypress|Ctrl}}+{{Keypress|C}} is pressed. For users who wish to disable this, simply add the following to {{Filename|~/.inputrc}}:
+
Due to an update to {{Pkg|readline}}, the terminal now echoes {{Ic|^C}} after {{Keypress|Ctrl}}+{{Keypress|C}} is pressed. For users who wish to disable this, simply add the following to {{Ic|~/.inputrc}}:
 
  set echo-control-characters off
 
  set echo-control-characters off
 
=Resources=
 
* [http://www.gnu.org/s/readline/ Readline Documentation]
 

Revision as of 15:33, 13 June 2012

Summary help replacing me
Improving readline over its default capabilities.
Important pages
Official site
Related
Bash

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.

Command-line editing

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 ~/.bashrc:

set -o vi

You may find either vi or emacs cheat sheets useful.

History

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:

  • 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. Had history search been enabled, only past commands beginning with ls (the current input) would've been shown, in this case ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig.

You can enable this mode by adding the lines below 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 ~/.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:

$include /etc/inputrc

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

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

"\es":forward-search-history

History search

Avoid duplicates

If you repeat the same command several times, they will all be appended in your history. To prevent this, add to your ~/.bashrc:

export HISTCONTROL=ignoredups

Avoid whitespaces

To disable logging blank commands add this to your ~/.bashrc:

export HISTCONTROL=ignorespace

If your ~/.bashrc already contains

export HISTCONTROL=ignoredups

replace it with

export HISTCONTROL=ignoreboth

Macros

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 Template:Keypress+Template:Keypress. Readline will act as though Template:Keypress+Template:Keypress (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 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"

The next one prefixes the line with 'yes |' when pressing Template:Keypress+Template:Keypress+Template:Keypress, 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 Template:Keypress+Template:Keypress is pressed:

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

As a last example, quickly send a command in the background with Template:Keypress+Template:Keypress+Template:Keypress, discarding all of its output:

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

Tips and tricks

Disabling control echo

Due to an update to readline, the terminal now echoes ^C after Template:Keypress+Template:Keypress is pressed. For users who wish to disable this, simply add the following to ~/.inputrc:

set echo-control-characters off