1 @comment %**start of header (This is for running Texinfo on a region.)
2 @setfilename rluser.info
3 @comment %**end of header (This is for running Texinfo on a region.)
6 This file documents the end user interface to the GNU command line
7 editing features. It is to be an appendix to manuals for programs which
8 use these features. There is a document entitled "readline.texinfo"
9 which contains both end-user and programmer documentation for the
12 Copyright (C) 1988--2009 Free Software Foundation, Inc.
14 Authored by Brian Fox and Chet Ramey.
16 Permission is granted to process this file through Tex and print the
17 results, provided the printed document carries copying permission notice
18 identical to this one except for the removal of this paragraph (this
19 paragraph not being relevant to the printed manual).
21 Permission is granted to make and distribute verbatim copies of this manual
22 provided the copyright notice and this permission notice are preserved on
25 Permission is granted to copy and distribute modified versions of this
26 manual under the conditions for verbatim copying, provided also that the
27 GNU Copyright statement is available to the distributee, and provided that
28 the entire resulting derived work is distributed under the terms of a
29 permission notice identical to this one.
31 Permission is granted to copy and distribute translations of this manual
32 into another language, under the above conditions for modified versions.
35 @comment If you are including this manual as an appendix, then set the
36 @comment variable readline-appendix.
42 @node Command Line Editing
43 @chapter Command Line Editing
45 This chapter describes the basic features of the @sc{gnu}
46 command line editing interface.
48 Command line editing is provided by the Readline library, which is
49 used by several different programs, including Bash.
50 Command line editing is enabled by default when using an interactive shell,
51 unless the @option{--noediting} option is supplied at shell invocation.
52 Line editing is also used when using the @option{-e} option to the
53 @code{read} builtin command (@pxref{Bash Builtins}).
54 By default, the line editing commands are similar to those of emacs.
55 A vi-style line editing interface is also available.
56 Line editing can be enabled at any time using the @option{-o emacs} or
57 @option{-o vi} options to the @code{set} builtin command
58 (@pxref{The Set Builtin}), or disabled using the @option{+o emacs} or
59 @option{+o vi} options to @code{set}.
63 * Introduction and Notation:: Notation used in this text.
64 * Readline Interaction:: The minimum set of commands for editing a line.
65 * Readline Init File:: Customizing Readline from a user's view.
66 * Bindable Readline Commands:: A description of most of the Readline commands
68 * Readline vi Mode:: A short description of how to make Readline
69 behave like the vi editor.
71 * Programmable Completion:: How to specify the possible completions for
73 * Programmable Completion Builtins:: Builtin commands to specify how to
74 complete arguments for a particular command.
78 @node Introduction and Notation
79 @section Introduction to Line Editing
81 The following paragraphs describe the notation used to represent
84 The text @kbd{C-k} is read as `Control-K' and describes the character
85 produced when the @key{k} key is pressed while the Control key
88 The text @kbd{M-k} is read as `Meta-K' and describes the character
89 produced when the Meta key (if you have one) is depressed, and the @key{k}
91 The Meta key is labeled @key{ALT} on many keyboards.
92 On keyboards with two keys labeled @key{ALT} (usually to either side of
93 the space bar), the @key{ALT} on the left side is generally set to
95 The @key{ALT} key on the right may also be configured to work as a
96 Meta key or may be configured as some other modifier, such as a
97 Compose key for typing accented characters.
99 If you do not have a Meta or @key{ALT} key, or another key working as
100 a Meta key, the identical keystroke can be generated by typing @key{ESC}
101 @emph{first}, and then typing @key{k}.
102 Either process is known as @dfn{metafying} the @key{k} key.
104 The text @kbd{M-C-k} is read as `Meta-Control-k' and describes the
105 character produced by @dfn{metafying} @kbd{C-k}.
107 In addition, several keys have their own names. Specifically,
108 @key{DEL}, @key{ESC}, @key{LFD}, @key{SPC}, @key{RET}, and @key{TAB} all
109 stand for themselves when seen in this text, or in an init file
110 (@pxref{Readline Init File}).
111 If your keyboard lacks a @key{LFD} key, typing @key{C-j} will
112 produce the desired character.
113 The @key{RET} key may be labeled @key{Return} or @key{Enter} on
116 @node Readline Interaction
117 @section Readline Interaction
118 @cindex interaction, readline
120 Often during an interactive session you type in a long line of text,
121 only to notice that the first word on the line is misspelled. The
122 Readline library gives you a set of commands for manipulating the text
123 as you type it in, allowing you to just fix your typo, and not forcing
124 you to retype the majority of the line. Using these editing commands,
125 you move the cursor to the place that needs correction, and delete or
126 insert the text of the corrections. Then, when you are satisfied with
127 the line, you simply press @key{RET}. You do not have to be at the
128 end of the line to press @key{RET}; the entire line is accepted
129 regardless of the location of the cursor within the line.
132 * Readline Bare Essentials:: The least you need to know about Readline.
133 * Readline Movement Commands:: Moving about the input line.
134 * Readline Killing Commands:: How to delete text, and how to get it back!
135 * Readline Arguments:: Giving numeric arguments to commands.
136 * Searching:: Searching through previous lines.
139 @node Readline Bare Essentials
140 @subsection Readline Bare Essentials
141 @cindex notation, readline
142 @cindex command editing
143 @cindex editing command lines
145 In order to enter characters into the line, simply type them. The typed
146 character appears where the cursor was, and then the cursor moves one
147 space to the right. If you mistype a character, you can use your
148 erase character to back up and delete the mistyped character.
150 Sometimes you may mistype a character, and
151 not notice the error until you have typed several other characters. In
152 that case, you can type @kbd{C-b} to move the cursor to the left, and then
153 correct your mistake. Afterwards, you can move the cursor to the right
156 When you add text in the middle of a line, you will notice that characters
157 to the right of the cursor are `pushed over' to make room for the text
158 that you have inserted. Likewise, when you delete text behind the cursor,
159 characters to the right of the cursor are `pulled back' to fill in the
160 blank space created by the removal of the text. A list of the bare
161 essentials for editing the text of an input line follows.
165 Move back one character.
167 Move forward one character.
168 @item @key{DEL} or @key{Backspace}
169 Delete the character to the left of the cursor.
171 Delete the character underneath the cursor.
172 @item @w{Printing characters}
173 Insert the character into the line at the cursor.
174 @item @kbd{C-_} or @kbd{C-x C-u}
175 Undo the last editing command. You can undo all the way back to an
180 (Depending on your configuration, the @key{Backspace} key be set to
181 delete the character to the left of the cursor and the @key{DEL} key set
182 to delete the character underneath the cursor, like @kbd{C-d}, rather
183 than the character to the left of the cursor.)
185 @node Readline Movement Commands
186 @subsection Readline Movement Commands
189 The above table describes the most basic keystrokes that you need
190 in order to do editing of the input line. For your convenience, many
191 other commands have been added in addition to @kbd{C-b}, @kbd{C-f},
192 @kbd{C-d}, and @key{DEL}. Here are some commands for moving more rapidly
197 Move to the start of the line.
199 Move to the end of the line.
201 Move forward a word, where a word is composed of letters and digits.
203 Move backward a word.
205 Clear the screen, reprinting the current line at the top.
208 Notice how @kbd{C-f} moves forward a character, while @kbd{M-f} moves
209 forward a word. It is a loose convention that control keystrokes
210 operate on characters while meta keystrokes operate on words.
212 @node Readline Killing Commands
213 @subsection Readline Killing Commands
218 @dfn{Killing} text means to delete the text from the line, but to save
219 it away for later use, usually by @dfn{yanking} (re-inserting)
220 it back into the line.
221 (`Cut' and `paste' are more recent jargon for `kill' and `yank'.)
223 If the description for a command says that it `kills' text, then you can
224 be sure that you can get the text back in a different (or the same)
227 When you use a kill command, the text is saved in a @dfn{kill-ring}.
228 Any number of consecutive kills save all of the killed text together, so
229 that when you yank it back, you get it all. The kill
230 ring is not line specific; the text that you killed on a previously
231 typed line is available to be yanked back later, when you are typing
235 Here is the list of commands for killing text.
239 Kill the text from the current cursor position to the end of the line.
242 Kill from the cursor to the end of the current word, or, if between
243 words, to the end of the next word.
244 Word boundaries are the same as those used by @kbd{M-f}.
247 Kill from the cursor the start of the current word, or, if between
248 words, to the start of the previous word.
249 Word boundaries are the same as those used by @kbd{M-b}.
252 Kill from the cursor to the previous whitespace. This is different than
253 @kbd{M-@key{DEL}} because the word boundaries differ.
257 Here is how to @dfn{yank} the text back into the line. Yanking
258 means to copy the most-recently-killed text from the kill buffer.
262 Yank the most recently killed text back into the buffer at the cursor.
265 Rotate the kill-ring, and yank the new top. You can only do this if
266 the prior command is @kbd{C-y} or @kbd{M-y}.
269 @node Readline Arguments
270 @subsection Readline Arguments
272 You can pass numeric arguments to Readline commands. Sometimes the
273 argument acts as a repeat count, other times it is the @i{sign} of the
274 argument that is significant. If you pass a negative argument to a
275 command which normally acts in a forward direction, that command will
276 act in a backward direction. For example, to kill text back to the
277 start of the line, you might type @samp{M-- C-k}.
279 The general way to pass numeric arguments to a command is to type meta
280 digits before the command. If the first `digit' typed is a minus
281 sign (@samp{-}), then the sign of the argument will be negative. Once
282 you have typed one meta digit to get the argument started, you can type
283 the remainder of the digits, and then the command. For example, to give
284 the @kbd{C-d} command an argument of 10, you could type @samp{M-1 0 C-d},
285 which will delete the next ten characters on the input line.
288 @subsection Searching for Commands in the History
290 Readline provides commands for searching through the command history
292 (@pxref{Bash History Facilities})
294 for lines containing a specified string.
295 There are two search modes: @dfn{incremental} and @dfn{non-incremental}.
297 Incremental searches begin before the user has finished typing the
299 As each character of the search string is typed, Readline displays
300 the next entry from the history matching the string typed so far.
301 An incremental search requires only as many characters as needed to
302 find the desired history entry.
303 To search backward in the history for a particular string, type
304 @kbd{C-r}. Typing @kbd{C-s} searches forward through the history.
305 The characters present in the value of the @code{isearch-terminators} variable
306 are used to terminate an incremental search.
307 If that variable has not been assigned a value, the @key{ESC} and
308 @kbd{C-J} characters will terminate an incremental search.
309 @kbd{C-g} will abort an incremental search and restore the original line.
310 When the search is terminated, the history entry containing the
311 search string becomes the current line.
313 To find other matching entries in the history list, type @kbd{C-r} or
314 @kbd{C-s} as appropriate.
315 This will search backward or forward in the history for the next
316 entry matching the search string typed so far.
317 Any other key sequence bound to a Readline command will terminate
318 the search and execute that command.
319 For instance, a @key{RET} will terminate the search and accept
320 the line, thereby executing the command from the history list.
321 A movement command will terminate the search, make the last line found
322 the current line, and begin editing.
324 Readline remembers the last incremental search string. If two
325 @kbd{C-r}s are typed without any intervening characters defining a new
326 search string, any remembered search string is used.
328 Non-incremental searches read the entire search string before starting
329 to search for matching history lines. The search string may be
330 typed by the user or be part of the contents of the current line.
332 @node Readline Init File
333 @section Readline Init File
334 @cindex initialization file, readline
336 Although the Readline library comes with a set of Emacs-like
337 keybindings installed by default, it is possible to use a different set
339 Any user can customize programs that use Readline by putting
340 commands in an @dfn{inputrc} file, conventionally in his home directory.
343 file is taken from the value of the shell variable @env{INPUTRC}. If
345 @ifclear BashFeatures
346 file is taken from the value of the environment variable @env{INPUTRC}. If
348 that variable is unset, the default is @file{~/.inputrc}. If that
349 file does not exist or cannot be read, the ultimate default is
352 When a program which uses the Readline library starts up, the
353 init file is read, and the key bindings are set.
355 In addition, the @code{C-x C-r} command re-reads this init file, thus
356 incorporating any changes that you might have made to it.
359 * Readline Init File Syntax:: Syntax for the commands in the inputrc file.
361 * Conditional Init Constructs:: Conditional key bindings in the inputrc file.
363 * Sample Init File:: An example inputrc file.
366 @node Readline Init File Syntax
367 @subsection Readline Init File Syntax
369 There are only a few basic constructs allowed in the
370 Readline init file. Blank lines are ignored.
371 Lines beginning with a @samp{#} are comments.
372 Lines beginning with a @samp{$} indicate conditional
373 constructs (@pxref{Conditional Init Constructs}). Other lines
374 denote variable settings and key bindings.
377 @item Variable Settings
378 You can modify the run-time behavior of Readline by
379 altering the values of variables in Readline
380 using the @code{set} command within the init file.
381 The syntax is simple:
384 set @var{variable} @var{value}
388 Here, for example, is how to
389 change from the default Emacs-like key binding to use
390 @code{vi} line editing commands:
396 Variable names and values, where appropriate, are recognized without regard
397 to case. Unrecognized variable names are ignored.
399 Boolean variables (those that can be set to on or off) are set to on if
400 the value is null or empty, @var{on} (case-insensitive), or 1. Any other
401 value results in the variable being set to off.
404 The @w{@code{bind -V}} command lists the current Readline variable names
405 and values. @xref{Bash Builtins}.
408 A great deal of run-time behavior is changeable with the following
411 @cindex variables, readline
416 Controls what happens when Readline wants to ring the terminal bell.
417 If set to @samp{none}, Readline never rings the bell. If set to
418 @samp{visible}, Readline uses a visible bell if one is available.
419 If set to @samp{audible} (the default), Readline attempts to ring
422 @item bind-tty-special-chars
423 @vindex bind-tty-special-chars
424 If set to @samp{on}, Readline attempts to bind the control characters
425 treated specially by the kernel's terminal driver to their Readline
429 @vindex comment-begin
430 The string to insert at the beginning of the line when the
431 @code{insert-comment} command is executed. The default value
434 @item completion-ignore-case
435 If set to @samp{on}, Readline performs filename matching and completion
436 in a case-insensitive fashion.
437 The default value is @samp{off}.
439 @item completion-prefix-display-length
440 @vindex completion-prefix-display-length
441 The length in characters of the common prefix of a list of possible
442 completions that is displayed without modification. When set to a
443 value greater than zero, common prefixes longer than this value are
444 replaced with an ellipsis when displaying possible completions.
446 @item completion-query-items
447 @vindex completion-query-items
448 The number of possible completions that determines when the user is
449 asked whether the list of possibilities should be displayed.
450 If the number of possible completions is greater than this value,
451 Readline will ask the user whether or not he wishes to view
452 them; otherwise, they are simply listed.
453 This variable must be set to an integer value greater than or equal to 0.
454 A negative value means Readline should never ask.
455 The default limit is @code{100}.
459 If set to @samp{on}, Readline will convert characters with the
460 eighth bit set to an @sc{ascii} key sequence by stripping the eighth
461 bit and prefixing an @key{ESC} character, converting them to a
462 meta-prefixed key sequence. The default value is @samp{on}.
464 @item disable-completion
465 @vindex disable-completion
466 If set to @samp{On}, Readline will inhibit word completion.
467 Completion characters will be inserted into the line as if they had
468 been mapped to @code{self-insert}. The default is @samp{off}.
472 The @code{editing-mode} variable controls which default set of
473 key bindings is used. By default, Readline starts up in Emacs editing
474 mode, where the keystrokes are most similar to Emacs. This variable can be
475 set to either @samp{emacs} or @samp{vi}.
477 @item echo-control-characters
478 When set to @samp{on}, on operating systems that indicate they support it,
479 readline echoes a character corresponding to a signal generated from the
480 keyboard. The default is @samp{on}.
483 @vindex enable-keypad
484 When set to @samp{on}, Readline will try to enable the application
485 keypad when it is called. Some systems need this to enable the
486 arrow keys. The default is @samp{off}.
488 @item enable-meta-key
489 When set to @samp{on}, Readline will try to enable any meta modifier
490 key the terminal claims to support when it is called. On many terminals,
491 the meta key is used to send eight-bit characters.
492 The default is @samp{on}.
496 If set to @samp{on}, tilde expansion is performed when Readline
497 attempts word completion. The default is @samp{off}.
499 @item history-preserve-point
500 @vindex history-preserve-point
501 If set to @samp{on}, the history code attempts to place the point (the
502 current cursor position) at the
503 same location on each history line retrieved with @code{previous-history}
504 or @code{next-history}. The default is @samp{off}.
508 Set the maximum number of history entries saved in the history list. If
509 set to zero, the number of entries in the history list is not limited.
511 @item horizontal-scroll-mode
512 @vindex horizontal-scroll-mode
513 This variable can be set to either @samp{on} or @samp{off}. Setting it
514 to @samp{on} means that the text of the lines being edited will scroll
515 horizontally on a single screen line when they are longer than the width
516 of the screen, instead of wrapping onto a new screen line. By default,
517 this variable is set to @samp{off}.
522 If set to @samp{on}, Readline will enable eight-bit input (it
523 will not clear the eighth bit in the characters it reads),
524 regardless of what the terminal claims it can support. The
525 default value is @samp{off}. The name @code{meta-flag} is a
526 synonym for this variable.
528 @item isearch-terminators
529 @vindex isearch-terminators
530 The string of characters that should terminate an incremental search without
531 subsequently executing the character as a command (@pxref{Searching}).
532 If this variable has not been given a value, the characters @key{ESC} and
533 @kbd{C-J} will terminate an incremental search.
537 Sets Readline's idea of the current keymap for key binding commands.
538 Acceptable @code{keymap} names are
540 @code{emacs-standard},
545 @code{vi-command}, and
547 @code{vi} is equivalent to @code{vi-command}; @code{emacs} is
548 equivalent to @code{emacs-standard}. The default value is @code{emacs}.
549 The value of the @code{editing-mode} variable also affects the
552 @item mark-directories
553 If set to @samp{on}, completed directory names have a slash
554 appended. The default is @samp{on}.
556 @item mark-modified-lines
557 @vindex mark-modified-lines
558 This variable, when set to @samp{on}, causes Readline to display an
559 asterisk (@samp{*}) at the start of history lines which have been modified.
560 This variable is @samp{off} by default.
562 @item mark-symlinked-directories
563 @vindex mark-symlinked-directories
564 If set to @samp{on}, completed names which are symbolic links
565 to directories have a slash appended (subject to the value of
566 @code{mark-directories}).
567 The default is @samp{off}.
569 @item match-hidden-files
570 @vindex match-hidden-files
571 This variable, when set to @samp{on}, causes Readline to match files whose
572 names begin with a @samp{.} (hidden files) when performing filename
573 completion, unless the leading @samp{.} is
574 supplied by the user in the filename to be completed.
575 This variable is @samp{on} by default.
579 If set to @samp{on}, Readline will display characters with the
580 eighth bit set directly rather than as a meta-prefixed escape
581 sequence. The default is @samp{off}.
583 @item page-completions
584 @vindex page-completions
585 If set to @samp{on}, Readline uses an internal @code{more}-like pager
586 to display a screenful of possible completions at a time.
587 This variable is @samp{on} by default.
589 @item print-completions-horizontally
590 If set to @samp{on}, Readline will display completions with matches
591 sorted horizontally in alphabetical order, rather than down the screen.
592 The default is @samp{off}.
594 @item revert-all-at-newline
595 @vindex revert-all-at-newline
596 If set to @samp{on}, Readline will undo all changes to history lines
597 before returning when @code{accept-line} is executed. By default,
598 history lines may be modified and retain individual undo lists across
599 calls to @code{readline}. The default is @samp{off}.
601 @item show-all-if-ambiguous
602 @vindex show-all-if-ambiguous
603 This alters the default behavior of the completion functions. If
605 words which have more than one possible completion cause the
606 matches to be listed immediately instead of ringing the bell.
607 The default value is @samp{off}.
609 @item show-all-if-unmodified
610 @vindex show-all-if-unmodified
611 This alters the default behavior of the completion functions in
612 a fashion similar to @var{show-all-if-ambiguous}.
614 words which have more than one possible completion without any
615 possible partial completion (the possible completions don't share
616 a common prefix) cause the matches to be listed immediately instead
618 The default value is @samp{off}.
620 @item skip-completed-text
621 @vindex skip-completed-text
622 If set to @samp{on}, this alters the default completion behavior when
623 inserting a single match into the line. It's only active when
624 performing completion in the middle of a word. If enabled, readline
625 does not insert characters from the completion that match characters
626 after point in the word being completed, so portions of the word
627 following the cursor are not duplicated.
628 For instance, if this is enabled, attempting completion when the cursor
629 is after the @samp{e} in @samp{Makefile} will result in @samp{Makefile}
630 rather than @samp{Makefilefile}, assuming there is a single possible
632 The default value is @samp{off}.
635 @vindex visible-stats
636 If set to @samp{on}, a character denoting a file's type
637 is appended to the filename when listing possible
638 completions. The default is @samp{off}.
643 The syntax for controlling key bindings in the init file is
644 simple. First you need to find the name of the command that you
645 want to change. The following sections contain tables of the command
646 name, the default keybinding, if any, and a short description of what
649 Once you know the name of the command, simply place on a line
650 in the init file the name of the key
651 you wish to bind the command to, a colon, and then the name of the
653 There can be no space between the key name and the colon -- that will be
654 interpreted as part of the key name.
655 The name of the key can be expressed in different ways, depending on
656 what you find most comfortable.
658 In addition to command names, readline allows keys to be bound
659 to a string that is inserted when the key is pressed (a @var{macro}).
662 The @w{@code{bind -p}} command displays Readline function names and
663 bindings in a format that can put directly into an initialization file.
664 @xref{Bash Builtins}.
668 @item @w{@var{keyname}: @var{function-name} or @var{macro}}
669 @var{keyname} is the name of a key spelled out in English. For example:
671 Control-u: universal-argument
672 Meta-Rubout: backward-kill-word
673 Control-o: "> output"
676 In the above example, @kbd{C-u} is bound to the function
677 @code{universal-argument},
678 @kbd{M-DEL} is bound to the function @code{backward-kill-word}, and
679 @kbd{C-o} is bound to run the macro
680 expressed on the right hand side (that is, to insert the text
681 @samp{> output} into the line).
683 A number of symbolic character names are recognized while
684 processing this key binding syntax:
698 @item @w{"@var{keyseq}": @var{function-name} or @var{macro}}
699 @var{keyseq} differs from @var{keyname} above in that strings
700 denoting an entire key sequence can be specified, by placing
701 the key sequence in double quotes. Some @sc{gnu} Emacs style key
702 escapes can be used, as in the following example, but the
703 special character names are not recognized.
706 "\C-u": universal-argument
707 "\C-x\C-r": re-read-init-file
708 "\e[11~": "Function Key 1"
711 In the above example, @kbd{C-u} is again bound to the function
712 @code{universal-argument} (just as it was in the first example),
713 @samp{@kbd{C-x} @kbd{C-r}} is bound to the function @code{re-read-init-file},
714 and @samp{@key{ESC} @key{[} @key{1} @key{1} @key{~}} is bound to insert
715 the text @samp{Function Key 1}.
719 The following @sc{gnu} Emacs style escape sequences are available when
720 specifying key sequences:
732 @key{"}, a double quotation mark
734 @key{'}, a single quote or apostrophe
737 In addition to the @sc{gnu} Emacs style escape sequences, a second
738 set of backslash escapes is available:
758 the eight-bit character whose value is the octal value @var{nnn}
759 (one to three digits)
761 the eight-bit character whose value is the hexadecimal value @var{HH}
762 (one or two hex digits)
765 When entering the text of a macro, single or double quotes must
766 be used to indicate a macro definition.
767 Unquoted text is assumed to be a function name.
768 In the macro body, the backslash escapes described above are expanded.
769 Backslash will quote any other character in the macro text,
770 including @samp{"} and @samp{'}.
771 For example, the following binding will make @samp{@kbd{C-x} \}
772 insert a single @samp{\} into the line:
779 @node Conditional Init Constructs
780 @subsection Conditional Init Constructs
782 Readline implements a facility similar in spirit to the conditional
783 compilation features of the C preprocessor which allows key
784 bindings and variable settings to be performed as the result
785 of tests. There are four parser directives used.
789 The @code{$if} construct allows bindings to be made based on the
790 editing mode, the terminal being used, or the application using
791 Readline. The text of the test extends to the end of the line;
792 no characters are required to isolate it.
796 The @code{mode=} form of the @code{$if} directive is used to test
797 whether Readline is in @code{emacs} or @code{vi} mode.
798 This may be used in conjunction
799 with the @samp{set keymap} command, for instance, to set bindings in
800 the @code{emacs-standard} and @code{emacs-ctlx} keymaps only if
801 Readline is starting out in @code{emacs} mode.
804 The @code{term=} form may be used to include terminal-specific
805 key bindings, perhaps to bind the key sequences output by the
806 terminal's function keys. The word on the right side of the
807 @samp{=} is tested against both the full name of the terminal and
808 the portion of the terminal name before the first @samp{-}. This
809 allows @code{sun} to match both @code{sun} and @code{sun-cmd},
813 The @var{application} construct is used to include
814 application-specific settings. Each program using the Readline
815 library sets the @var{application name}, and you can test for
817 This could be used to bind key sequences to functions useful for
818 a specific program. For instance, the following command adds a
819 key sequence that quotes the current or previous word in Bash:
822 # Quote the current or previous word
823 "\C-xq": "\eb\"\ef\""
829 This command, as seen in the previous example, terminates an
833 Commands in this branch of the @code{$if} directive are executed if
837 This directive takes a single filename as an argument and reads commands
838 and bindings from that file.
839 For example, the following directive reads from @file{/etc/inputrc}:
841 $include /etc/inputrc
845 @node Sample Init File
846 @subsection Sample Init File
848 Here is an example of an @var{inputrc} file. This illustrates key
849 binding, variable assignment, and conditional syntax.
853 # This file controls the behaviour of line input editing for
854 # programs that use the GNU Readline library. Existing
855 # programs include FTP, Bash, and GDB.
857 # You can re-read the inputrc file with C-x C-r.
858 # Lines beginning with '#' are comments.
860 # First, include any systemwide bindings and variable
861 # assignments from /etc/Inputrc
862 $include /etc/Inputrc
865 # Set various bindings for emacs mode.
867 set editing-mode emacs
871 Meta-Control-h: backward-kill-word Text after the function name is ignored
874 # Arrow keys in keypad mode
876 #"\M-OD": backward-char
877 #"\M-OC": forward-char
878 #"\M-OA": previous-history
879 #"\M-OB": next-history
881 # Arrow keys in ANSI mode
883 "\M-[D": backward-char
884 "\M-[C": forward-char
885 "\M-[A": previous-history
886 "\M-[B": next-history
888 # Arrow keys in 8 bit keypad mode
890 #"\M-\C-OD": backward-char
891 #"\M-\C-OC": forward-char
892 #"\M-\C-OA": previous-history
893 #"\M-\C-OB": next-history
895 # Arrow keys in 8 bit ANSI mode
897 #"\M-\C-[D": backward-char
898 #"\M-\C-[C": forward-char
899 #"\M-\C-[A": previous-history
900 #"\M-\C-[B": next-history
906 # An old-style binding. This happens to be the default.
909 # Macros that are convenient for shell interaction
912 "\C-xp": "PATH=$@{PATH@}\e\C-e\C-a\ef\C-f"
913 # prepare to type a quoted word --
914 # insert open and close double quotes
915 # and move to just after the open quote
917 # insert a backslash (testing backslash escapes
918 # in sequences and macros)
920 # Quote the current or previous word
921 "\C-xq": "\eb\"\ef\""
922 # Add a binding to refresh the line, which is unbound
923 "\C-xr": redraw-current-line
924 # Edit variable on current line.
925 "\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y="
928 # use a visible bell if one is available
929 set bell-style visible
931 # don't strip characters to 7 bits when reading
934 # allow iso-latin1 characters to be inserted rather
935 # than converted to prefix-meta sequences
938 # display characters with the eighth bit set directly
939 # rather than as meta-prefixed characters
942 # if there are more than 150 possible completions for
943 # a word, ask the user if he wants to see all of them
944 set completion-query-items 150
950 "\M-.": yank-last-arg
954 @node Bindable Readline Commands
955 @section Bindable Readline Commands
958 * Commands For Moving:: Moving about the line.
959 * Commands For History:: Getting at previous lines.
960 * Commands For Text:: Commands for changing text.
961 * Commands For Killing:: Commands for killing and yanking.
962 * Numeric Arguments:: Specifying numeric arguments, repeat counts.
963 * Commands For Completion:: Getting Readline to do the typing for you.
964 * Keyboard Macros:: Saving and re-executing typed characters
965 * Miscellaneous Commands:: Other miscellaneous commands.
968 This section describes Readline commands that may be bound to key
971 You can list your key bindings by executing
972 @w{@code{bind -P}} or, for a more terse format, suitable for an
973 @var{inputrc} file, @w{@code{bind -p}}. (@xref{Bash Builtins}.)
975 Command names without an accompanying key sequence are unbound by default.
977 In the following descriptions, @dfn{point} refers to the current cursor
978 position, and @dfn{mark} refers to a cursor position saved by the
979 @code{set-mark} command.
980 The text between the point and mark is referred to as the @dfn{region}.
982 @node Commands For Moving
983 @subsection Commands For Moving
985 @item beginning-of-line (C-a)
986 Move to the start of the current line.
988 @item end-of-line (C-e)
989 Move to the end of the line.
991 @item forward-char (C-f)
992 Move forward a character.
994 @item backward-char (C-b)
995 Move back a character.
997 @item forward-word (M-f)
998 Move forward to the end of the next word.
999 Words are composed of letters and digits.
1001 @item backward-word (M-b)
1002 Move back to the start of the current or previous word.
1003 Words are composed of letters and digits.
1006 @item shell-forward-word ()
1007 Move forward to the end of the next word.
1008 Words are delimited by non-quoted shell metacharacters.
1010 @item shell-backward-word ()
1011 Move back to the start of the current or previous word.
1012 Words are delimited by non-quoted shell metacharacters.
1015 @item clear-screen (C-l)
1016 Clear the screen and redraw the current line,
1017 leaving the current line at the top of the screen.
1019 @item redraw-current-line ()
1020 Refresh the current line. By default, this is unbound.
1024 @node Commands For History
1025 @subsection Commands For Manipulating The History
1028 @item accept-line (Newline or Return)
1030 Accept the line regardless of where the cursor is.
1032 non-empty, add it to the history list according to the setting of
1033 the @env{HISTCONTROL} and @env{HISTIGNORE} variables.
1034 If this line is a modified history line, then restore the history line
1035 to its original state.
1037 @ifclear BashFeatures
1038 Accept the line regardless of where the cursor is.
1040 non-empty, it may be added to the history list for future recall with
1041 @code{add_history()}.
1042 If this line is a modified history line, the history line is restored
1043 to its original state.
1046 @item previous-history (C-p)
1047 Move `back' through the history list, fetching the previous command.
1049 @item next-history (C-n)
1050 Move `forward' through the history list, fetching the next command.
1052 @item beginning-of-history (M-<)
1053 Move to the first line in the history.
1055 @item end-of-history (M->)
1056 Move to the end of the input history, i.e., the line currently
1059 @item reverse-search-history (C-r)
1060 Search backward starting at the current line and moving `up' through
1061 the history as necessary. This is an incremental search.
1063 @item forward-search-history (C-s)
1064 Search forward starting at the current line and moving `down' through
1065 the the history as necessary. This is an incremental search.
1067 @item non-incremental-reverse-search-history (M-p)
1068 Search backward starting at the current line and moving `up'
1069 through the history as necessary using a non-incremental search
1070 for a string supplied by the user.
1072 @item non-incremental-forward-search-history (M-n)
1073 Search forward starting at the current line and moving `down'
1074 through the the history as necessary using a non-incremental search
1075 for a string supplied by the user.
1077 @item history-search-forward ()
1078 Search forward through the history for the string of characters
1079 between the start of the current line and the point.
1080 This is a non-incremental search.
1081 By default, this command is unbound.
1083 @item history-search-backward ()
1084 Search backward through the history for the string of characters
1085 between the start of the current line and the point. This
1086 is a non-incremental search. By default, this command is unbound.
1088 @item yank-nth-arg (M-C-y)
1089 Insert the first argument to the previous command (usually
1090 the second word on the previous line) at point.
1091 With an argument @var{n},
1092 insert the @var{n}th word from the previous command (the words
1093 in the previous command begin with word 0). A negative argument
1094 inserts the @var{n}th word from the end of the previous command.
1095 Once the argument @var{n} is computed, the argument is extracted
1096 as if the @samp{!@var{n}} history expansion had been specified.
1098 @item yank-last-arg (M-. or M-_)
1099 Insert last argument to the previous command (the last word of the
1100 previous history entry). With an
1101 argument, behave exactly like @code{yank-nth-arg}.
1102 Successive calls to @code{yank-last-arg} move back through the history
1103 list, inserting the last argument of each line in turn.
1104 The history expansion facilities are used to extract the last argument,
1105 as if the @samp{!$} history expansion had been specified.
1109 @node Commands For Text
1110 @subsection Commands For Changing Text
1113 @item delete-char (C-d)
1114 Delete the character at point. If point is at the
1115 beginning of the line, there are no characters in the line, and
1116 the last character typed was not bound to @code{delete-char}, then
1119 @item backward-delete-char (Rubout)
1120 Delete the character behind the cursor. A numeric argument means
1121 to kill the characters instead of deleting them.
1123 @item forward-backward-delete-char ()
1124 Delete the character under the cursor, unless the cursor is at the
1125 end of the line, in which case the character behind the cursor is
1126 deleted. By default, this is not bound to a key.
1128 @item quoted-insert (C-q or C-v)
1129 Add the next character typed to the line verbatim. This is
1130 how to insert key sequences like @kbd{C-q}, for example.
1132 @ifclear BashFeatures
1133 @item tab-insert (M-@key{TAB})
1134 Insert a tab character.
1137 @item self-insert (a, b, A, 1, !, @dots{})
1140 @item transpose-chars (C-t)
1141 Drag the character before the cursor forward over
1142 the character at the cursor, moving the
1143 cursor forward as well. If the insertion point
1144 is at the end of the line, then this
1145 transposes the last two characters of the line.
1146 Negative arguments have no effect.
1148 @item transpose-words (M-t)
1149 Drag the word before point past the word after point,
1150 moving point past that word as well.
1151 If the insertion point is at the end of the line, this transposes
1152 the last two words on the line.
1154 @item upcase-word (M-u)
1155 Uppercase the current (or following) word. With a negative argument,
1156 uppercase the previous word, but do not move the cursor.
1158 @item downcase-word (M-l)
1159 Lowercase the current (or following) word. With a negative argument,
1160 lowercase the previous word, but do not move the cursor.
1162 @item capitalize-word (M-c)
1163 Capitalize the current (or following) word. With a negative argument,
1164 capitalize the previous word, but do not move the cursor.
1166 @item overwrite-mode ()
1167 Toggle overwrite mode. With an explicit positive numeric argument,
1168 switches to overwrite mode. With an explicit non-positive numeric
1169 argument, switches to insert mode. This command affects only
1170 @code{emacs} mode; @code{vi} mode does overwrite differently.
1171 Each call to @code{readline()} starts in insert mode.
1173 In overwrite mode, characters bound to @code{self-insert} replace
1174 the text at point rather than pushing the text to the right.
1175 Characters bound to @code{backward-delete-char} replace the character
1176 before point with a space.
1178 By default, this command is unbound.
1182 @node Commands For Killing
1183 @subsection Killing And Yanking
1187 @item kill-line (C-k)
1188 Kill the text from point to the end of the line.
1190 @item backward-kill-line (C-x Rubout)
1191 Kill backward to the beginning of the line.
1193 @item unix-line-discard (C-u)
1194 Kill backward from the cursor to the beginning of the current line.
1196 @item kill-whole-line ()
1197 Kill all characters on the current line, no matter where point is.
1198 By default, this is unbound.
1200 @item kill-word (M-d)
1201 Kill from point to the end of the current word, or if between
1202 words, to the end of the next word.
1203 Word boundaries are the same as @code{forward-word}.
1205 @item backward-kill-word (M-@key{DEL})
1206 Kill the word behind point.
1207 Word boundaries are the same as @code{backward-word}.
1210 @item shell-kill-word ()
1211 Kill from point to the end of the current word, or if between
1212 words, to the end of the next word.
1213 Word boundaries are the same as @code{shell-forward-word}.
1215 @item backward-kill-word ()
1216 Kill the word behind point.
1217 Word boundaries are the same as @code{shell-backward-word}.
1220 @item unix-word-rubout (C-w)
1221 Kill the word behind point, using white space as a word boundary.
1222 The killed text is saved on the kill-ring.
1224 @item unix-filename-rubout ()
1225 Kill the word behind point, using white space and the slash character
1226 as the word boundaries.
1227 The killed text is saved on the kill-ring.
1229 @item delete-horizontal-space ()
1230 Delete all spaces and tabs around point. By default, this is unbound.
1232 @item kill-region ()
1233 Kill the text in the current region.
1234 By default, this command is unbound.
1236 @item copy-region-as-kill ()
1237 Copy the text in the region to the kill buffer, so it can be yanked
1238 right away. By default, this command is unbound.
1240 @item copy-backward-word ()
1241 Copy the word before point to the kill buffer.
1242 The word boundaries are the same as @code{backward-word}.
1243 By default, this command is unbound.
1245 @item copy-forward-word ()
1246 Copy the word following point to the kill buffer.
1247 The word boundaries are the same as @code{forward-word}.
1248 By default, this command is unbound.
1251 Yank the top of the kill ring into the buffer at point.
1253 @item yank-pop (M-y)
1254 Rotate the kill-ring, and yank the new top. You can only do this if
1255 the prior command is @code{yank} or @code{yank-pop}.
1258 @node Numeric Arguments
1259 @subsection Specifying Numeric Arguments
1262 @item digit-argument (@kbd{M-0}, @kbd{M-1}, @dots{} @kbd{M--})
1263 Add this digit to the argument already accumulating, or start a new
1264 argument. @kbd{M--} starts a negative argument.
1266 @item universal-argument ()
1267 This is another way to specify an argument.
1268 If this command is followed by one or more digits, optionally with a
1269 leading minus sign, those digits define the argument.
1270 If the command is followed by digits, executing @code{universal-argument}
1271 again ends the numeric argument, but is otherwise ignored.
1272 As a special case, if this command is immediately followed by a
1273 character that is neither a digit or minus sign, the argument count
1274 for the next command is multiplied by four.
1275 The argument count is initially one, so executing this function the
1276 first time makes the argument count four, a second time makes the
1277 argument count sixteen, and so on.
1278 By default, this is not bound to a key.
1281 @node Commands For Completion
1282 @subsection Letting Readline Type For You
1285 @item complete (@key{TAB})
1286 Attempt to perform completion on the text before point.
1287 The actual completion performed is application-specific.
1289 Bash attempts completion treating the text as a variable (if the
1290 text begins with @samp{$}), username (if the text begins with
1291 @samp{~}), hostname (if the text begins with @samp{@@}), or
1292 command (including aliases and functions) in turn. If none
1293 of these produces a match, filename completion is attempted.
1295 @ifclear BashFeatures
1296 The default is filename completion.
1299 @item possible-completions (M-?)
1300 List the possible completions of the text before point.
1302 @item insert-completions (M-*)
1303 Insert all completions of the text before point that would have
1304 been generated by @code{possible-completions}.
1306 @item menu-complete ()
1307 Similar to @code{complete}, but replaces the word to be completed
1308 with a single match from the list of possible completions.
1309 Repeated execution of @code{menu-complete} steps through the list
1310 of possible completions, inserting each match in turn.
1311 At the end of the list of completions, the bell is rung
1312 (subject to the setting of @code{bell-style})
1313 and the original text is restored.
1314 An argument of @var{n} moves @var{n} positions forward in the list
1315 of matches; a negative argument may be used to move backward
1317 This command is intended to be bound to @key{TAB}, but is unbound
1320 @item menu-complete-backward ()
1321 Identical to @code{menu-complete}, but moves backward through the list
1322 of possible completions, as if @code{menu-complete} had been given a
1325 @item delete-char-or-list ()
1326 Deletes the character under the cursor if not at the beginning or
1327 end of the line (like @code{delete-char}).
1328 If at the end of the line, behaves identically to
1329 @code{possible-completions}.
1330 This command is unbound by default.
1333 @item complete-filename (M-/)
1334 Attempt filename completion on the text before point.
1336 @item possible-filename-completions (C-x /)
1337 List the possible completions of the text before point,
1338 treating it as a filename.
1340 @item complete-username (M-~)
1341 Attempt completion on the text before point, treating
1344 @item possible-username-completions (C-x ~)
1345 List the possible completions of the text before point,
1346 treating it as a username.
1348 @item complete-variable (M-$)
1349 Attempt completion on the text before point, treating
1350 it as a shell variable.
1352 @item possible-variable-completions (C-x $)
1353 List the possible completions of the text before point,
1354 treating it as a shell variable.
1356 @item complete-hostname (M-@@)
1357 Attempt completion on the text before point, treating
1360 @item possible-hostname-completions (C-x @@)
1361 List the possible completions of the text before point,
1362 treating it as a hostname.
1364 @item complete-command (M-!)
1365 Attempt completion on the text before point, treating
1366 it as a command name. Command completion attempts to
1367 match the text against aliases, reserved words, shell
1368 functions, shell builtins, and finally executable filenames,
1371 @item possible-command-completions (C-x !)
1372 List the possible completions of the text before point,
1373 treating it as a command name.
1375 @item dynamic-complete-history (M-@key{TAB})
1376 Attempt completion on the text before point, comparing
1377 the text against lines from the history list for possible
1380 @item dabbrev-expand ()
1381 Attempt menu completion on the text before point, comparing
1382 the text against lines from the history list for possible
1385 @item complete-into-braces (M-@{)
1386 Perform filename completion and insert the list of possible completions
1387 enclosed within braces so the list is available to the shell
1388 (@pxref{Brace Expansion}).
1393 @node Keyboard Macros
1394 @subsection Keyboard Macros
1397 @item start-kbd-macro (C-x ()
1398 Begin saving the characters typed into the current keyboard macro.
1400 @item end-kbd-macro (C-x ))
1401 Stop saving the characters typed into the current keyboard macro
1402 and save the definition.
1404 @item call-last-kbd-macro (C-x e)
1405 Re-execute the last keyboard macro defined, by making the characters
1406 in the macro appear as if typed at the keyboard.
1410 @node Miscellaneous Commands
1411 @subsection Some Miscellaneous Commands
1414 @item re-read-init-file (C-x C-r)
1415 Read in the contents of the @var{inputrc} file, and incorporate
1416 any bindings or variable assignments found there.
1419 Abort the current editing command and
1420 ring the terminal's bell (subject to the setting of
1423 @item do-uppercase-version (M-a, M-b, M-@var{x}, @dots{})
1424 If the metafied character @var{x} is lowercase, run the command
1425 that is bound to the corresponding uppercase character.
1427 @item prefix-meta (@key{ESC})
1428 Metafy the next character typed. This is for keyboards
1429 without a meta key. Typing @samp{@key{ESC} f} is equivalent to typing
1432 @item undo (C-_ or C-x C-u)
1433 Incremental undo, separately remembered for each line.
1435 @item revert-line (M-r)
1436 Undo all changes made to this line. This is like executing the @code{undo}
1437 command enough times to get back to the beginning.
1440 @item tilde-expand (M-&)
1442 @ifclear BashFeatures
1443 @item tilde-expand (M-~)
1445 Perform tilde expansion on the current word.
1447 @item set-mark (C-@@)
1448 Set the mark to the point. If a
1449 numeric argument is supplied, the mark is set to that position.
1451 @item exchange-point-and-mark (C-x C-x)
1452 Swap the point with the mark. The current cursor position is set to
1453 the saved position, and the old cursor position is saved as the mark.
1455 @item character-search (C-])
1456 A character is read and point is moved to the next occurrence of that
1457 character. A negative count searches for previous occurrences.
1459 @item character-search-backward (M-C-])
1460 A character is read and point is moved to the previous occurrence
1461 of that character. A negative count searches for subsequent
1464 @item skip-csi-sequence ()
1465 Read enough characters to consume a multi-key sequence such as those
1466 defined for keys like Home and End. Such sequences begin with a
1467 Control Sequence Indicator (CSI), usually ESC-[. If this sequence is
1468 bound to "\e[", keys producing such sequences will have no effect
1469 unless explicitly bound to a readline command, instead of inserting
1470 stray characters into the editing buffer. This is unbound by default,
1471 but usually bound to ESC-[.
1473 @item insert-comment (M-#)
1474 Without a numeric argument, the value of the @code{comment-begin}
1475 variable is inserted at the beginning of the current line.
1476 If a numeric argument is supplied, this command acts as a toggle: if
1477 the characters at the beginning of the line do not match the value
1478 of @code{comment-begin}, the value is inserted, otherwise
1479 the characters in @code{comment-begin} are deleted from the beginning of
1481 In either case, the line is accepted as if a newline had been typed.
1483 The default value of @code{comment-begin} causes this command
1484 to make the current line a shell comment.
1485 If a numeric argument causes the comment character to be removed, the line
1486 will be executed by the shell.
1489 @item dump-functions ()
1490 Print all of the functions and their key bindings to the
1491 Readline output stream. If a numeric argument is supplied,
1492 the output is formatted in such a way that it can be made part
1493 of an @var{inputrc} file. This command is unbound by default.
1495 @item dump-variables ()
1496 Print all of the settable variables and their values to the
1497 Readline output stream. If a numeric argument is supplied,
1498 the output is formatted in such a way that it can be made part
1499 of an @var{inputrc} file. This command is unbound by default.
1501 @item dump-macros ()
1502 Print all of the Readline key sequences bound to macros and the
1503 strings they output. If a numeric argument is supplied,
1504 the output is formatted in such a way that it can be made part
1505 of an @var{inputrc} file. This command is unbound by default.
1508 @item glob-complete-word (M-g)
1509 The word before point is treated as a pattern for pathname expansion,
1510 with an asterisk implicitly appended. This pattern is used to
1511 generate a list of matching file names for possible completions.
1513 @item glob-expand-word (C-x *)
1514 The word before point is treated as a pattern for pathname expansion,
1515 and the list of matching file names is inserted, replacing the word.
1516 If a numeric argument is supplied, a @samp{*} is appended before
1519 @item glob-list-expansions (C-x g)
1520 The list of expansions that would have been generated by
1521 @code{glob-expand-word} is displayed, and the line is redrawn.
1522 If a numeric argument is supplied, a @samp{*} is appended before
1525 @item display-shell-version (C-x C-v)
1526 Display version information about the current instance of Bash.
1528 @item shell-expand-line (M-C-e)
1529 Expand the line as the shell does.
1530 This performs alias and history expansion as well as all of the shell
1531 word expansions (@pxref{Shell Expansions}).
1533 @item history-expand-line (M-^)
1534 Perform history expansion on the current line.
1536 @item magic-space ()
1537 Perform history expansion on the current line and insert a space
1538 (@pxref{History Interaction}).
1540 @item alias-expand-line ()
1541 Perform alias expansion on the current line (@pxref{Aliases}).
1543 @item history-and-alias-expand-line ()
1544 Perform history and alias expansion on the current line.
1546 @item insert-last-argument (M-. or M-_)
1547 A synonym for @code{yank-last-arg}.
1549 @item operate-and-get-next (C-o)
1550 Accept the current line for execution and fetch the next line
1551 relative to the current line from the history for editing. Any
1552 argument is ignored.
1554 @item edit-and-execute-command (C-xC-e)
1555 Invoke an editor on the current command line, and execute the result as shell
1557 Bash attempts to invoke
1558 @code{$VISUAL}, @code{$EDITOR}, and @code{emacs}
1559 as the editor, in that order.
1563 @ifclear BashFeatures
1564 @item emacs-editing-mode (C-e)
1565 When in @code{vi} command mode, this causes a switch to @code{emacs}
1568 @item vi-editing-mode (M-C-j)
1569 When in @code{emacs} editing mode, this causes a switch to @code{vi}
1576 @node Readline vi Mode
1577 @section Readline vi Mode
1579 While the Readline library does not have a full set of @code{vi}
1580 editing functions, it does contain enough to allow simple editing
1581 of the line. The Readline @code{vi} mode behaves as specified in
1582 the @sc{posix} 1003.2 standard.
1585 In order to switch interactively between @code{emacs} and @code{vi}
1586 editing modes, use the @samp{set -o emacs} and @samp{set -o vi}
1587 commands (@pxref{The Set Builtin}).
1589 @ifclear BashFeatures
1590 In order to switch interactively between @code{emacs} and @code{vi}
1591 editing modes, use the command @kbd{M-C-j} (bound to emacs-editing-mode
1592 when in @code{vi} mode and to vi-editing-mode in @code{emacs} mode).
1594 The Readline default is @code{emacs} mode.
1596 When you enter a line in @code{vi} mode, you are already placed in
1597 `insertion' mode, as if you had typed an @samp{i}. Pressing @key{ESC}
1598 switches you into `command' mode, where you can edit the text of the
1599 line with the standard @code{vi} movement keys, move to previous
1600 history lines with @samp{k} and subsequent lines with @samp{j}, and
1604 @node Programmable Completion
1605 @section Programmable Completion
1606 @cindex programmable completion
1608 When word completion is attempted for an argument to a command for
1609 which a completion specification (a @var{compspec}) has been defined
1610 using the @code{complete} builtin (@pxref{Programmable Completion Builtins}),
1611 the programmable completion facilities are invoked.
1613 First, the command name is identified.
1614 If a compspec has been defined for that command, the
1615 compspec is used to generate the list of possible completions for the word.
1616 If the command word is the empty string (completion attempted at the
1617 beginning of an empty line), any compspec defined with
1618 the @option{-E} option to @code{complete} is used.
1619 If the command word is a full pathname, a compspec for the full
1620 pathname is searched for first.
1621 If no compspec is found for the full pathname, an attempt is made to
1622 find a compspec for the portion following the final slash.
1623 If those searches do not result in a compspec, any compspec defined with
1624 the @option{-D} option to @code{complete} is used as the default.
1626 Once a compspec has been found, it is used to generate the list of
1628 If a compspec is not found, the default Bash completion
1629 described above (@pxref{Commands For Completion}) is performed.
1631 First, the actions specified by the compspec are used.
1632 Only matches which are prefixed by the word being completed are
1634 When the @option{-f} or @option{-d} option is used for filename or
1635 directory name completion, the shell variable @env{FIGNORE} is
1636 used to filter the matches.
1637 @xref{Bash Variables}, for a description of @env{FIGNORE}.
1639 Any completions specified by a filename expansion pattern to the
1640 @option{-G} option are generated next.
1641 The words generated by the pattern need not match the word being completed.
1642 The @env{GLOBIGNORE} shell variable is not used to filter the matches,
1643 but the @env{FIGNORE} shell variable is used.
1645 Next, the string specified as the argument to the @option{-W} option
1647 The string is first split using the characters in the @env{IFS}
1648 special variable as delimiters.
1649 Shell quoting is honored.
1650 Each word is then expanded using
1651 brace expansion, tilde expansion, parameter and variable expansion,
1652 command substitution, and arithmetic expansion,
1653 as described above (@pxref{Shell Expansions}).
1654 The results are split using the rules described above
1655 (@pxref{Word Splitting}).
1656 The results of the expansion are prefix-matched against the word being
1657 completed, and the matching words become the possible completions.
1659 After these matches have been generated, any shell function or command
1660 specified with the @option{-F} and @option{-C} options is invoked.
1661 When the command or function is invoked, the @env{COMP_LINE},
1662 @env{COMP_POINT}, @env{COMP_KEY}, and @env{COMP_TYPE} variables are
1663 assigned values as described above (@pxref{Bash Variables}).
1664 If a shell function is being invoked, the @env{COMP_WORDS} and
1665 @env{COMP_CWORD} variables are also set.
1666 When the function or command is invoked, the first argument is the
1667 name of the command whose arguments are being completed, the
1668 second argument is the word being completed, and the third argument
1669 is the word preceding the word being completed on the current command line.
1670 No filtering of the generated completions against the word being completed
1671 is performed; the function or command has complete freedom in generating
1674 Any function specified with @option{-F} is invoked first.
1675 The function may use any of the shell facilities, including the
1676 @code{compgen} and @code{compopt} builtins described below
1677 (@pxref{Programmable Completion Builtins}), to generate the matches.
1678 It must put the possible completions in the @env{COMPREPLY} array
1681 Next, any command specified with the @option{-C} option is invoked
1682 in an environment equivalent to command substitution.
1683 It should print a list of completions, one per line, to
1684 the standard output.
1685 Backslash may be used to escape a newline, if necessary.
1687 After all of the possible completions are generated, any filter
1688 specified with the @option{-X} option is applied to the list.
1689 The filter is a pattern as used for pathname expansion; a @samp{&}
1690 in the pattern is replaced with the text of the word being completed.
1691 A literal @samp{&} may be escaped with a backslash; the backslash
1692 is removed before attempting a match.
1693 Any completion that matches the pattern will be removed from the list.
1694 A leading @samp{!} negates the pattern; in this case any completion
1695 not matching the pattern will be removed.
1697 Finally, any prefix and suffix specified with the @option{-P} and @option{-S}
1698 options are added to each member of the completion list, and the result is
1699 returned to the Readline completion code as the list of possible
1702 If the previously-applied actions do not generate any matches, and the
1703 @option{-o dirnames} option was supplied to @code{complete} when the
1704 compspec was defined, directory name completion is attempted.
1706 If the @option{-o plusdirs} option was supplied to @code{complete} when
1707 the compspec was defined, directory name completion is attempted and any
1708 matches are added to the results of the other actions.
1710 By default, if a compspec is found, whatever it generates is returned to
1711 the completion code as the full set of possible completions.
1712 The default Bash completions are not attempted, and the Readline default
1713 of filename completion is disabled.
1714 If the @option{-o bashdefault} option was supplied to @code{complete} when
1715 the compspec was defined, the default Bash completions are attempted
1716 if the compspec generates no matches.
1717 If the @option{-o default} option was supplied to @code{complete} when the
1718 compspec was defined, Readline's default completion will be performed
1719 if the compspec (and, if attempted, the default Bash completions)
1720 generate no matches.
1722 When a compspec indicates that directory name completion is desired,
1723 the programmable completion functions force Readline to append a slash
1724 to completed names which are symbolic links to directories, subject to
1725 the value of the @var{mark-directories} Readline variable, regardless
1726 of the setting of the @var{mark-symlinked-directories} Readline variable.
1728 There is some support for dynamically modifying completions. This is
1729 most useful when used in combination with a default completion specified
1730 with @option{-D}. It's possible for shell functions executed as completion
1731 handlers to indicate that completion should be retried by returning an
1732 exit status of 124. If a shell function returns 124, and changes
1733 the compspec associated with the command on which completion is being
1734 attempted (supplied as the first argument when the function is executed),
1735 programmable completion restarts from the beginning, with an
1736 attempt to find a compspec for that command. This allows a set of
1737 completions to be built dynamically as completion is attempted, rather than
1738 being loaded all at once.
1740 For instance, assuming that there is a library of compspecs, each kept in a
1741 file corresponding to the name of the command, the following default
1742 completion function would load completions dynamically:
1745 _completion_loader()
1747 . "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return 124
1749 complete -D -F _completion_loader
1752 @node Programmable Completion Builtins
1753 @section Programmable Completion Builtins
1754 @cindex completion builtins
1756 Two builtin commands are available to manipulate the programmable completion
1763 @code{compgen [@var{option}] [@var{word}]}
1766 Generate possible completion matches for @var{word} according to
1767 the @var{option}s, which may be any option accepted by the
1769 builtin with the exception of @option{-p} and @option{-r}, and write
1770 the matches to the standard output.
1771 When using the @option{-F} or @option{-C} options, the various shell variables
1772 set by the programmable completion facilities, while available, will not
1775 The matches will be generated in the same way as if the programmable
1776 completion code had generated them directly from a completion specification
1777 with the same flags.
1778 If @var{word} is specified, only those completions matching @var{word}
1781 The return value is true unless an invalid option is supplied, or no
1782 matches were generated.
1787 @code{complete [-abcdefgjksuv] [-o @var{comp-option}] [-DE] [-A @var{action}] [-G @var{globpat}] [-W @var{wordlist}]
1788 [-F @var{function}] [-C @var{command}] [-X @var{filterpat}]
1789 [-P @var{prefix}] [-S @var{suffix}] @var{name} [@var{name} @dots{}]}
1790 @code{complete -pr [-DE] [@var{name} @dots{}]}
1793 Specify how arguments to each @var{name} should be completed.
1794 If the @option{-p} option is supplied, or if no options are supplied, existing
1795 completion specifications are printed in a way that allows them to be
1797 The @option{-r} option removes a completion specification for
1798 each @var{name}, or, if no @var{name}s are supplied, all
1799 completion specifications.
1800 The @option{-D} option indicates that the remaining options and actions should
1801 apply to the ``default'' command completion; that is, completion attempted
1802 on a command for which no completion has previously been defined.
1803 The @option{-E} option indicates that the remaining options and actions should
1804 apply to ``empty'' command completion; that is, completion attempted on a
1807 The process of applying these completion specifications when word completion
1808 is attempted is described above (@pxref{Programmable Completion}). The
1809 @option{-D} option takes precedence over @option{-E}.
1811 Other options, if specified, have the following meanings.
1812 The arguments to the @option{-G}, @option{-W}, and @option{-X} options
1813 (and, if necessary, the @option{-P} and @option{-S} options)
1814 should be quoted to protect them from expansion before the
1815 @code{complete} builtin is invoked.
1819 @item -o @var{comp-option}
1820 The @var{comp-option} controls several aspects of the compspec's behavior
1821 beyond the simple generation of completions.
1822 @var{comp-option} may be one of:
1827 Perform the rest of the default Bash completions if the compspec
1828 generates no matches.
1831 Use Readline's default filename completion if the compspec generates
1835 Perform directory name completion if the compspec generates no matches.
1838 Tell Readline that the compspec generates filenames, so it can perform any
1839 filename-specific processing (like adding a slash to directory names
1840 quoting special characters, or suppressing trailing spaces).
1841 This option is intended to be used with shell functions specified
1845 Tell Readline not to append a space (the default) to words completed at
1846 the end of the line.
1849 After any matches defined by the compspec are generated,
1850 directory name completion is attempted and any
1851 matches are added to the results of the other actions.
1855 @item -A @var{action}
1856 The @var{action} may be one of the following to generate a list of possible
1861 Alias names. May also be specified as @option{-a}.
1864 Array variable names.
1867 Readline key binding names (@pxref{Bindable Readline Commands}).
1870 Names of shell builtin commands. May also be specified as @option{-b}.
1873 Command names. May also be specified as @option{-c}.
1876 Directory names. May also be specified as @option{-d}.
1879 Names of disabled shell builtins.
1882 Names of enabled shell builtins.
1885 Names of exported shell variables. May also be specified as @option{-e}.
1888 File names. May also be specified as @option{-f}.
1891 Names of shell functions.
1894 Group names. May also be specified as @option{-g}.
1897 Help topics as accepted by the @code{help} builtin (@pxref{Bash Builtins}).
1900 Hostnames, as taken from the file specified by the
1901 @env{HOSTFILE} shell variable (@pxref{Bash Variables}).
1904 Job names, if job control is active. May also be specified as @option{-j}.
1907 Shell reserved words. May also be specified as @option{-k}.
1910 Names of running jobs, if job control is active.
1913 Service names. May also be specified as @option{-s}.
1916 Valid arguments for the @option{-o} option to the @code{set} builtin
1917 (@pxref{The Set Builtin}).
1920 Shell option names as accepted by the @code{shopt} builtin
1921 (@pxref{Bash Builtins}).
1927 Names of stopped jobs, if job control is active.
1930 User names. May also be specified as @option{-u}.
1933 Names of all shell variables. May also be specified as @option{-v}.
1936 @item -G @var{globpat}
1937 The filename expansion pattern @var{globpat} is expanded to generate
1938 the possible completions.
1940 @item -W @var{wordlist}
1941 The @var{wordlist} is split using the characters in the
1942 @env{IFS} special variable as delimiters, and each resultant word
1944 The possible completions are the members of the resultant list which
1945 match the word being completed.
1947 @item -C @var{command}
1948 @var{command} is executed in a subshell environment, and its output is
1949 used as the possible completions.
1951 @item -F @var{function}
1952 The shell function @var{function} is executed in the current shell
1954 When it finishes, the possible completions are retrieved from the value
1955 of the @env{COMPREPLY} array variable.
1957 @item -X @var{filterpat}
1958 @var{filterpat} is a pattern as used for filename expansion.
1959 It is applied to the list of possible completions generated by the
1960 preceding options and arguments, and each completion matching
1961 @var{filterpat} is removed from the list.
1962 A leading @samp{!} in @var{filterpat} negates the pattern; in this
1963 case, any completion not matching @var{filterpat} is removed.
1965 @item -P @var{prefix}
1966 @var{prefix} is added at the beginning of each possible completion
1967 after all other options have been applied.
1969 @item -S @var{suffix}
1970 @var{suffix} is appended to each possible completion
1971 after all other options have been applied.
1974 The return value is true unless an invalid option is supplied, an option
1975 other than @option{-p} or @option{-r} is supplied without a @var{name}
1976 argument, an attempt is made to remove a completion specification for
1977 a @var{name} for which no specification exists, or
1978 an error occurs adding a completion specification.
1983 @code{compopt} [-o @var{option}] [-DE] [+o @var{option}] [@var{name}]
1985 Modify completion options for each @var{name} according to the
1986 @var{option}s, or for the currently-execution completion if no @var{name}s
1988 If no @var{option}s are given, display the completion options for each
1989 @var{name} or the current completion.
1990 The possible values of @var{option} are those valid for the @code{complete}
1991 builtin described above.
1992 The @option{-D} option indicates that the remaining options should
1993 apply to the ``default'' command completion; that is, completion attempted
1994 on a command for which no completion has previously been defined.
1995 The @option{-E} option indicates that the remaining options should
1996 apply to ``empty'' command completion; that is, completion attempted on a
1999 The @option{-D} option takes precedence over @option{-E}.
2001 The return value is true unless an invalid option is supplied, an attempt
2002 is made to modify the options for a @var{name} for which no completion
2003 specification exists, or an output error occurs.