Difference between revisions of "Readline"

From ArchWiki
Jump to: navigation, search
m (Mode indicator in prompt: fix defaults)
 
(36 intermediate revisions by 16 users not shown)
Line 1: Line 1:
[[Category:Command shells]]  
+
[[Category:Command-line]]
 
[[it:Readline]]
 
[[it:Readline]]
[[zh-CN:Readline]]
+
[[ja:Readline]]
{{Article summary start}}
+
[[zh-hans:Readline]]
{{Article summary text|Improving readline over its default capabilities.}}
+
[[ru:Readline]]
{{Article summary heading|Important pages}}
+
[http://www.gnu.org/s/readline/ readline] is a library by the [[GNU Project]], used by [[Bash]] and other CLI-interface programs to edit and interact with the command line. See {{man|3|readline}} for more information.
{{Article summary link|Official site|http://www.gnu.org/s/readline/}}
 
{{Article summary heading|Related}}
 
{{Article summary wiki|Bash}}
 
{{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 [http://www.gnu.org/s/readline/ page] as only subtle configuration will be introduced here.
+
== Editing mode ==
  
== Command-line editing ==
+
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 {{ic|~/.inputrc}}:
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
+
{{hc|~/.inputrc|
 +
set editing-mode vi}}
 +
 
 +
Alternatively, to set it only for [[bash]] by adding the following line to {{ic|~/.bashrc}}:
 +
 
 +
{{hc|~/.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:
 +
 
 +
{{hc|~/.inputrc|
 +
set show-mode-in-prompt on
 +
}}
 +
 
 +
This will print a string in your prompt ({{ic|(cmd)}}/{{ic|(ins)}} by default) that can be customized with the {{ic|vi-ins-mode-string}} and {{ic|vi-cmd-mode-string}} variables.
 +
 
 +
=== Different cursor shapes for each mode ===
 +
 
 +
You can set a different cursor shape for each mode by using [https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File-Syntax.html#index-vi_002dcmd_002dmode_002dstring "\1 .. \2" escapes]:
 +
 
 +
{{hc|~/.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.
 +
 
 +
The Virtual Console uses different escape codes, so you should check first which term is being used:
 +
 
 +
{{hc|~/.inputrc|2=
 +
$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 [https://www.kernel.org/doc/Documentation/admin-guide/vga-softcursor.rst software cursor for VGA] for further details.
 +
 
 +
== Fast word movement ==
 +
 
 +
[[Xterm]] supports moving between words with {{ic|Ctrl+Left}} and {{ic|Ctrl+Right}} [http://stackoverflow.com/a/7783928 by default]. To achieve this effect with other terminal emulators, find the correct [http://wiki.bash-hackers.org/scripting/terminalcodes terminal codes], and bind them to {{ic|backward-word}} and {{ic|forward-word}} in {{ic|~/.inputrc}}.
  
Alternatively, to use the vi-like keybindings, you may add the following line to your {{Ic|~/.bashrc}}:
+
For example, for [[urxvt]]:
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.
+
{{hc|~/.inputrc|
 +
"\e[1;5D": backward-word
 +
"\e[1;5C": forward-word}}
  
 
== History ==
 
== History ==
Line 32: Line 74:
  
 
You can enable the history search mode by adding the following lines to {{Ic|/etc/inputrc}} or {{Ic|~/.inputrc}}:
 
You can enable the history search mode by adding the following lines 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 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]):
 
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]):
Line 42: Line 84:
 
  "\e[A": history-search-backward
 
  "\e[A": history-search-backward
 
  "\e[B": history-search-forward
 
  "\e[B": history-search-forward
 +
j: history-search-forward
 +
k: history-search-backward
 
  set keymap vi-insert
 
  set keymap vi-insert
 
  # these are for vi-insert mode
 
  # these are for vi-insert mode
Line 57: Line 101:
 
  $ stty -ixon
 
  $ stty -ixon
 
or use a different key in {{Ic|inputrc}}. For example, to use {{ic|Alt+S}} which is not bound by default:
 
or use a different key in {{Ic|inputrc}}. For example, to use {{ic|Alt+S}} which is not bound by default:
  "\es":forward-search-history
+
  "\es": forward-search-history
  
===History search===
+
== Faster completion ==
==== Avoid duplicates ====
 
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
 
  
==== Avoid whitespaces ====
+
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.
To disable logging of lines beginning with a space add this to your {{Ic|~/.bashrc}}:
+
 
export HISTCONTROL=ignorespace
+
The double tab can be changed to a single tab by setting
If your {{Ic|~/.bashrc}} already contains
+
{{hc|~/.inputrc|
export HISTCONTROL=ignoredups
+
set show-all-if-unmodified on
replace it with
+
}}
export HISTCONTROL=ignoreboth
+
 
 +
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:
 +
{{hc|~/.inputrc|
 +
set show-all-if-ambiguous on
 +
}}
 +
 
 +
== Colorized completion ==
 +
 
 +
You can enable coloring of completion of filenames with the {{ic|colored-stats}} option. You can also color the identical prefix of completion-lists with {{ic|colored-completion-prefix}}. For example:
 +
 
 +
{{hc|~/.inputrc|
 +
# 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
 +
}}
  
 
== 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"'
  
 
or add the part within single quotes to inputrc:
 
or add the part within single quotes to inputrc:
  "\ew":"\C-e # macro"
+
  "\ew": "\C-e # macro"
  
 
Now type a line and press {{ic|Alt}}+{{ic|W}}. Readline will act as though {{ic|Ctrl+E}} (end-of-line) had been pressed, appended with '{{Ic| # macro}}'.
 
Now type a line and press {{ic|Alt}}+{{ic|W}}. Readline will act as though {{ic|Ctrl+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 {{ic|Ctrl+Alt+L}} append "| less" to the line and run it ({{ic|Ctrl+M}} is equivalent to {{ic|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 {{ic|Ctrl+Alt+L}} append "| less" to the line and run it ({{ic|Ctrl+M}} is equivalent to {{ic|Enter}}):
  "\e\C-l":"\C-e | less\C-m"
+
  "\e\C-l": "\C-e | less\C-m"
  
 
The next one prefixes the line with 'yes |' when pressing {{ic|Ctrl+Alt+Y}}, confirming any yes/no question the command might ask:
 
The next one prefixes the line with 'yes |' when pressing {{ic|Ctrl+Alt+Y}}, confirming any yes/no question the command might ask:
  "\e\C-y":"\C-ayes | \C-m"
+
  "\e\C-y": "\C-ayes | \C-m"
  
 
This example wraps the line in {{Ic|su -c ''}}, if {{ic|Alt+S}} is pressed:
 
This example wraps the line in {{Ic|su -c ''}}, if {{ic|Alt+S}} is pressed:
  "\es":"\C-a su -c '\C-e'\C-m"
+
  "\es": "\C-a su -c '\C-e'\C-m"
 +
 
 +
This example prefixes the line with {{Ic|sudo }}, if {{ic|Alt+S}} is pressed.  It's safer because it won't input the {{ic|Enter}} key.
 +
"\es": "\C-asudo \C-e"
  
 
As a last example, quickly send a command in the background with {{ic|Ctrl+Alt+B}}, discarding all of its output:
 
As a last example, quickly send a command in the background with {{ic|Ctrl+Alt+B}}, discarding all of its output:
  "\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 ==
+
== Disabling control echo ==
=== Disabling control echo ===
+
''readline'' causes the terminal to echoe {{Ic|^C}} after {{ic|Ctrl+C}} is pressed. For users who wish to disable this, simply add the following to {{Ic|~/.inputrc}}:
Due to an update to {{Pkg|readline}}, the terminal now echoes {{Ic|^C}} after {{ic|Ctrl+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
 +
 +
== See also ==
 +
 +
* [http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf vi readline editing cheat sheat]
 +
* [http://www.catonmat.net/download/readline-emacs-editing-mode-cheat-sheet.pdf emacs readline editing cheat sheet]

Latest revision as of 10:00, 10 June 2018

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

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.

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

History

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

$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 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:

"\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
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 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 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 '', 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 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"

Disabling control echo

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

set echo-control-characters off

See also