]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/doc/rluser.texi
bash-4.3-rc2 overlay
[thirdparty/bash.git] / lib / readline / doc / rluser.texi
CommitLineData
d3a24ed2
CR
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.)
d3a24ed2
CR
4
5@ignore
6This file documents the end user interface to the GNU command line
7editing features. It is to be an appendix to manuals for programs which
8use these features. There is a document entitled "readline.texinfo"
9which contains both end-user and programmer documentation for the
10GNU Readline Library.
11
b6e23235 12Copyright (C) 1988--2014 Free Software Foundation, Inc.
d3a24ed2
CR
13
14Authored by Brian Fox and Chet Ramey.
15
16Permission is granted to process this file through Tex and print the
17results, provided the printed document carries copying permission notice
18identical to this one except for the removal of this paragraph (this
19paragraph not being relevant to the printed manual).
20
21Permission is granted to make and distribute verbatim copies of this manual
22provided the copyright notice and this permission notice are preserved on
23all copies.
24
25Permission is granted to copy and distribute modified versions of this
26manual under the conditions for verbatim copying, provided also that the
27GNU Copyright statement is available to the distributee, and provided that
28the entire resulting derived work is distributed under the terms of a
29permission notice identical to this one.
30
31Permission is granted to copy and distribute translations of this manual
32into another language, under the above conditions for modified versions.
33@end ignore
34
35@comment If you are including this manual as an appendix, then set the
36@comment variable readline-appendix.
37
38@ifclear BashFeatures
39@defcodeindex bt
40@end ifclear
41
42@node Command Line Editing
43@chapter Command Line Editing
44
45This chapter describes the basic features of the @sc{gnu}
46command line editing interface.
47@ifset BashFeatures
48Command line editing is provided by the Readline library, which is
49used by several different programs, including Bash.
14e8b2a7
CR
50Command line editing is enabled by default when using an interactive shell,
51unless the @option{--noediting} option is supplied at shell invocation.
52Line editing is also used when using the @option{-e} option to the
53@code{read} builtin command (@pxref{Bash Builtins}).
dc60d4e0 54By default, the line editing commands are similar to those of Emacs.
14e8b2a7
CR
55A vi-style line editing interface is also available.
56Line 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}.
d3a24ed2
CR
60@end ifset
61
62@menu
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
67 available for binding
68* Readline vi Mode:: A short description of how to make Readline
69 behave like the vi editor.
70@ifset BashFeatures
71* Programmable Completion:: How to specify the possible completions for
72 a specific command.
73* Programmable Completion Builtins:: Builtin commands to specify how to
74 complete arguments for a particular command.
eb4206df
CR
75* A Programmable Completion Example:: An example shell function for
76 generating possible completions.
d3a24ed2
CR
77@end ifset
78@end menu
79
80@node Introduction and Notation
81@section Introduction to Line Editing
82
83The following paragraphs describe the notation used to represent
84keystrokes.
85
86The text @kbd{C-k} is read as `Control-K' and describes the character
87produced when the @key{k} key is pressed while the Control key
88is depressed.
89
90The text @kbd{M-k} is read as `Meta-K' and describes the character
91produced when the Meta key (if you have one) is depressed, and the @key{k}
92key is pressed.
93The Meta key is labeled @key{ALT} on many keyboards.
94On keyboards with two keys labeled @key{ALT} (usually to either side of
95the space bar), the @key{ALT} on the left side is generally set to
96work as a Meta key.
97The @key{ALT} key on the right may also be configured to work as a
98Meta key or may be configured as some other modifier, such as a
99Compose key for typing accented characters.
100
101If you do not have a Meta or @key{ALT} key, or another key working as
102a Meta key, the identical keystroke can be generated by typing @key{ESC}
103@emph{first}, and then typing @key{k}.
104Either process is known as @dfn{metafying} the @key{k} key.
105
106The text @kbd{M-C-k} is read as `Meta-Control-k' and describes the
107character produced by @dfn{metafying} @kbd{C-k}.
108
109In addition, several keys have their own names. Specifically,
110@key{DEL}, @key{ESC}, @key{LFD}, @key{SPC}, @key{RET}, and @key{TAB} all
111stand for themselves when seen in this text, or in an init file
112(@pxref{Readline Init File}).
113If your keyboard lacks a @key{LFD} key, typing @key{C-j} will
114produce the desired character.
115The @key{RET} key may be labeled @key{Return} or @key{Enter} on
116some keyboards.
117
118@node Readline Interaction
119@section Readline Interaction
120@cindex interaction, readline
121
122Often during an interactive session you type in a long line of text,
123only to notice that the first word on the line is misspelled. The
124Readline library gives you a set of commands for manipulating the text
125as you type it in, allowing you to just fix your typo, and not forcing
126you to retype the majority of the line. Using these editing commands,
127you move the cursor to the place that needs correction, and delete or
128insert the text of the corrections. Then, when you are satisfied with
129the line, you simply press @key{RET}. You do not have to be at the
130end of the line to press @key{RET}; the entire line is accepted
131regardless of the location of the cursor within the line.
132
133@menu
134* Readline Bare Essentials:: The least you need to know about Readline.
135* Readline Movement Commands:: Moving about the input line.
136* Readline Killing Commands:: How to delete text, and how to get it back!
137* Readline Arguments:: Giving numeric arguments to commands.
138* Searching:: Searching through previous lines.
139@end menu
140
141@node Readline Bare Essentials
142@subsection Readline Bare Essentials
143@cindex notation, readline
144@cindex command editing
145@cindex editing command lines
146
147In order to enter characters into the line, simply type them. The typed
148character appears where the cursor was, and then the cursor moves one
149space to the right. If you mistype a character, you can use your
150erase character to back up and delete the mistyped character.
151
152Sometimes you may mistype a character, and
153not notice the error until you have typed several other characters. In
154that case, you can type @kbd{C-b} to move the cursor to the left, and then
155correct your mistake. Afterwards, you can move the cursor to the right
156with @kbd{C-f}.
157
158When you add text in the middle of a line, you will notice that characters
159to the right of the cursor are `pushed over' to make room for the text
160that you have inserted. Likewise, when you delete text behind the cursor,
161characters to the right of the cursor are `pulled back' to fill in the
162blank space created by the removal of the text. A list of the bare
163essentials for editing the text of an input line follows.
164
165@table @asis
166@item @kbd{C-b}
167Move back one character.
168@item @kbd{C-f}
169Move forward one character.
170@item @key{DEL} or @key{Backspace}
171Delete the character to the left of the cursor.
172@item @kbd{C-d}
173Delete the character underneath the cursor.
174@item @w{Printing characters}
175Insert the character into the line at the cursor.
176@item @kbd{C-_} or @kbd{C-x C-u}
177Undo the last editing command. You can undo all the way back to an
178empty line.
179@end table
180
181@noindent
182(Depending on your configuration, the @key{Backspace} key be set to
183delete the character to the left of the cursor and the @key{DEL} key set
184to delete the character underneath the cursor, like @kbd{C-d}, rather
185than the character to the left of the cursor.)
186
187@node Readline Movement Commands
188@subsection Readline Movement Commands
189
190
191The above table describes the most basic keystrokes that you need
192in order to do editing of the input line. For your convenience, many
193other commands have been added in addition to @kbd{C-b}, @kbd{C-f},
194@kbd{C-d}, and @key{DEL}. Here are some commands for moving more rapidly
195about the line.
196
197@table @kbd
198@item C-a
199Move to the start of the line.
200@item C-e
201Move to the end of the line.
202@item M-f
203Move forward a word, where a word is composed of letters and digits.
204@item M-b
205Move backward a word.
206@item C-l
207Clear the screen, reprinting the current line at the top.
208@end table
209
210Notice how @kbd{C-f} moves forward a character, while @kbd{M-f} moves
211forward a word. It is a loose convention that control keystrokes
212operate on characters while meta keystrokes operate on words.
213
214@node Readline Killing Commands
215@subsection Readline Killing Commands
216
217@cindex killing text
218@cindex yanking text
219
220@dfn{Killing} text means to delete the text from the line, but to save
221it away for later use, usually by @dfn{yanking} (re-inserting)
222it back into the line.
223(`Cut' and `paste' are more recent jargon for `kill' and `yank'.)
224
225If the description for a command says that it `kills' text, then you can
226be sure that you can get the text back in a different (or the same)
227place later.
228
229When you use a kill command, the text is saved in a @dfn{kill-ring}.
230Any number of consecutive kills save all of the killed text together, so
231that when you yank it back, you get it all. The kill
232ring is not line specific; the text that you killed on a previously
233typed line is available to be yanked back later, when you are typing
234another line.
235@cindex kill ring
236
237Here is the list of commands for killing text.
238
239@table @kbd
240@item C-k
241Kill the text from the current cursor position to the end of the line.
242
243@item M-d
244Kill from the cursor to the end of the current word, or, if between
245words, to the end of the next word.
246Word boundaries are the same as those used by @kbd{M-f}.
247
248@item M-@key{DEL}
249Kill from the cursor the start of the current word, or, if between
250words, to the start of the previous word.
251Word boundaries are the same as those used by @kbd{M-b}.
252
253@item C-w
254Kill from the cursor to the previous whitespace. This is different than
255@kbd{M-@key{DEL}} because the word boundaries differ.
256
257@end table
258
259Here is how to @dfn{yank} the text back into the line. Yanking
260means to copy the most-recently-killed text from the kill buffer.
261
262@table @kbd
263@item C-y
264Yank the most recently killed text back into the buffer at the cursor.
265
266@item M-y
267Rotate the kill-ring, and yank the new top. You can only do this if
268the prior command is @kbd{C-y} or @kbd{M-y}.
269@end table
270
271@node Readline Arguments
272@subsection Readline Arguments
273
274You can pass numeric arguments to Readline commands. Sometimes the
275argument acts as a repeat count, other times it is the @i{sign} of the
276argument that is significant. If you pass a negative argument to a
277command which normally acts in a forward direction, that command will
278act in a backward direction. For example, to kill text back to the
279start of the line, you might type @samp{M-- C-k}.
280
281The general way to pass numeric arguments to a command is to type meta
282digits before the command. If the first `digit' typed is a minus
283sign (@samp{-}), then the sign of the argument will be negative. Once
284you have typed one meta digit to get the argument started, you can type
285the remainder of the digits, and then the command. For example, to give
286the @kbd{C-d} command an argument of 10, you could type @samp{M-1 0 C-d},
287which will delete the next ten characters on the input line.
288
289@node Searching
290@subsection Searching for Commands in the History
291
292Readline provides commands for searching through the command history
293@ifset BashFeatures
294(@pxref{Bash History Facilities})
295@end ifset
296for lines containing a specified string.
297There are two search modes: @dfn{incremental} and @dfn{non-incremental}.
298
299Incremental searches begin before the user has finished typing the
300search string.
301As each character of the search string is typed, Readline displays
302the next entry from the history matching the string typed so far.
303An incremental search requires only as many characters as needed to
304find the desired history entry.
305To search backward in the history for a particular string, type
306@kbd{C-r}. Typing @kbd{C-s} searches forward through the history.
307The characters present in the value of the @code{isearch-terminators} variable
308are used to terminate an incremental search.
309If that variable has not been assigned a value, the @key{ESC} and
310@kbd{C-J} characters will terminate an incremental search.
311@kbd{C-g} will abort an incremental search and restore the original line.
312When the search is terminated, the history entry containing the
313search string becomes the current line.
314
315To find other matching entries in the history list, type @kbd{C-r} or
316@kbd{C-s} as appropriate.
317This will search backward or forward in the history for the next
318entry matching the search string typed so far.
319Any other key sequence bound to a Readline command will terminate
320the search and execute that command.
321For instance, a @key{RET} will terminate the search and accept
322the line, thereby executing the command from the history list.
323A movement command will terminate the search, make the last line found
324the current line, and begin editing.
325
326Readline remembers the last incremental search string. If two
327@kbd{C-r}s are typed without any intervening characters defining a new
328search string, any remembered search string is used.
329
330Non-incremental searches read the entire search string before starting
331to search for matching history lines. The search string may be
332typed by the user or be part of the contents of the current line.
333
334@node Readline Init File
335@section Readline Init File
336@cindex initialization file, readline
337
338Although the Readline library comes with a set of Emacs-like
339keybindings installed by default, it is possible to use a different set
340of keybindings.
341Any user can customize programs that use Readline by putting
342commands in an @dfn{inputrc} file, conventionally in his home directory.
343The name of this
344@ifset BashFeatures
345file is taken from the value of the shell variable @env{INPUTRC}. If
346@end ifset
347@ifclear BashFeatures
348file is taken from the value of the environment variable @env{INPUTRC}. If
349@end ifclear
8d618825
CR
350that variable is unset, the default is @file{~/.inputrc}. If that
351file does not exist or cannot be read, the ultimate default is
352@file{/etc/inputrc}.
d3a24ed2
CR
353
354When a program which uses the Readline library starts up, the
355init file is read, and the key bindings are set.
356
357In addition, the @code{C-x C-r} command re-reads this init file, thus
358incorporating any changes that you might have made to it.
359
360@menu
361* Readline Init File Syntax:: Syntax for the commands in the inputrc file.
362
363* Conditional Init Constructs:: Conditional key bindings in the inputrc file.
364
365* Sample Init File:: An example inputrc file.
366@end menu
367
368@node Readline Init File Syntax
369@subsection Readline Init File Syntax
370
371There are only a few basic constructs allowed in the
372Readline init file. Blank lines are ignored.
373Lines beginning with a @samp{#} are comments.
374Lines beginning with a @samp{$} indicate conditional
375constructs (@pxref{Conditional Init Constructs}). Other lines
376denote variable settings and key bindings.
377
378@table @asis
379@item Variable Settings
380You can modify the run-time behavior of Readline by
381altering the values of variables in Readline
382using the @code{set} command within the init file.
383The syntax is simple:
384
385@example
386set @var{variable} @var{value}
387@end example
388
389@noindent
390Here, for example, is how to
391change from the default Emacs-like key binding to use
392@code{vi} line editing commands:
393
394@example
395set editing-mode vi
396@end example
397
398Variable names and values, where appropriate, are recognized without regard
ff247e74
CR
399to case. Unrecognized variable names are ignored.
400
401Boolean variables (those that can be set to on or off) are set to on if
402the value is null or empty, @var{on} (case-insensitive), or 1. Any other
403value results in the variable being set to off.
d3a24ed2
CR
404
405@ifset BashFeatures
406The @w{@code{bind -V}} command lists the current Readline variable names
407and values. @xref{Bash Builtins}.
408@end ifset
409
410A great deal of run-time behavior is changeable with the following
411variables.
412
413@cindex variables, readline
414@table @code
415
416@item bell-style
417@vindex bell-style
418Controls what happens when Readline wants to ring the terminal bell.
419If set to @samp{none}, Readline never rings the bell. If set to
420@samp{visible}, Readline uses a visible bell if one is available.
421If set to @samp{audible} (the default), Readline attempts to ring
422the terminal's bell.
423
453f278a 424@item bind-tty-special-chars
eb2bb562 425@vindex bind-tty-special-chars
453f278a
CR
426If set to @samp{on}, Readline attempts to bind the control characters
427treated specially by the kernel's terminal driver to their Readline
428equivalents.
429
5a318736
CR
430@item colored-stats
431@vindex colored-stats
432If set to @samp{on}, Readline displays possible completions using different
433colors to indicate their file type.
434The color definitions are taken from the value of the @env{LS_COLORS}
435environment variable.
436The default is @samp{off}.
437
d3a24ed2
CR
438@item comment-begin
439@vindex comment-begin
440The string to insert at the beginning of the line when the
441@code{insert-comment} command is executed. The default value
442is @code{"#"}.
443
e502b4e0
CR
444@item completion-display-width
445@vindex completion-display-width
446The number of screen columns used to display possible matches
447when performing completion.
448The value is ignored if it is less than 0 or greater than the terminal
449screen width.
450A value of 0 will cause matches to be displayed one per line.
451The default value is -1.
452
d3a24ed2 453@item completion-ignore-case
e502b4e0 454@vindex completion-ignore-case
d3a24ed2
CR
455If set to @samp{on}, Readline performs filename matching and completion
456in a case-insensitive fashion.
457The default value is @samp{off}.
458
6faad625
CR
459@item completion-map-case
460@vindex completion-map-case
461If set to @samp{on}, and @var{completion-ignore-case} is enabled, Readline
462treats hyphens (@samp{-}) and underscores (@samp{_}) as equivalent when
463performing case-insensitive filename matching and completion.
464
f13513ff
CR
465@item completion-prefix-display-length
466@vindex completion-prefix-display-length
467The length in characters of the common prefix of a list of possible
468completions that is displayed without modification. When set to a
469value greater than zero, common prefixes longer than this value are
470replaced with an ellipsis when displaying possible completions.
471
d3a24ed2
CR
472@item completion-query-items
473@vindex completion-query-items
474The number of possible completions that determines when the user is
475asked whether the list of possibilities should be displayed.
476If the number of possible completions is greater than this value,
477Readline will ask the user whether or not he wishes to view
478them; otherwise, they are simply listed.
479This variable must be set to an integer value greater than or equal to 0.
66e6d7cf 480A negative value means Readline should never ask.
d3a24ed2
CR
481The default limit is @code{100}.
482
483@item convert-meta
484@vindex convert-meta
485If set to @samp{on}, Readline will convert characters with the
486eighth bit set to an @sc{ascii} key sequence by stripping the eighth
487bit and prefixing an @key{ESC} character, converting them to a
488meta-prefixed key sequence. The default value is @samp{on}.
489
490@item disable-completion
491@vindex disable-completion
492If set to @samp{On}, Readline will inhibit word completion.
493Completion characters will be inserted into the line as if they had
494been mapped to @code{self-insert}. The default is @samp{off}.
495
496@item editing-mode
497@vindex editing-mode
498The @code{editing-mode} variable controls which default set of
499key bindings is used. By default, Readline starts up in Emacs editing
500mode, where the keystrokes are most similar to Emacs. This variable can be
501set to either @samp{emacs} or @samp{vi}.
502
824dfe68
CR
503@item echo-control-characters
504When set to @samp{on}, on operating systems that indicate they support it,
505readline echoes a character corresponding to a signal generated from the
506keyboard. The default is @samp{on}.
507
d3a24ed2
CR
508@item enable-keypad
509@vindex enable-keypad
510When set to @samp{on}, Readline will try to enable the application
511keypad when it is called. Some systems need this to enable the
512arrow keys. The default is @samp{off}.
513
08e72d7a
CR
514@item enable-meta-key
515When set to @samp{on}, Readline will try to enable any meta modifier
516key the terminal claims to support when it is called. On many terminals,
517the meta key is used to send eight-bit characters.
518The default is @samp{on}.
519
d3a24ed2
CR
520@item expand-tilde
521@vindex expand-tilde
522If set to @samp{on}, tilde expansion is performed when Readline
523attempts word completion. The default is @samp{off}.
524
a2e7f358 525@item history-preserve-point
d3a24ed2 526@vindex history-preserve-point
d3ad40de
CR
527If set to @samp{on}, the history code attempts to place the point (the
528current cursor position) at the
d3a24ed2 529same location on each history line retrieved with @code{previous-history}
a2e7f358 530or @code{next-history}. The default is @samp{off}.
d3a24ed2 531
1d0e1a34
CR
532@item history-size
533@vindex history-size
0500de0b
CR
534Set the maximum number of history entries saved in the history list.
535If set to zero, any existing history entries are deleted and no new entries
536are saved.
537If set to a value less than zero, the number of history entries is not
538limited.
539By default, the number of history entries is not limited.
1d0e1a34 540
d3a24ed2
CR
541@item horizontal-scroll-mode
542@vindex horizontal-scroll-mode
543This variable can be set to either @samp{on} or @samp{off}. Setting it
544to @samp{on} means that the text of the lines being edited will scroll
545horizontally on a single screen line when they are longer than the width
546of the screen, instead of wrapping onto a new screen line. By default,
547this variable is set to @samp{off}.
548
549@item input-meta
550@vindex input-meta
551@vindex meta-flag
552If set to @samp{on}, Readline will enable eight-bit input (it
553will not clear the eighth bit in the characters it reads),
554regardless of what the terminal claims it can support. The
555default value is @samp{off}. The name @code{meta-flag} is a
556synonym for this variable.
557
558@item isearch-terminators
559@vindex isearch-terminators
560The string of characters that should terminate an incremental search without
561subsequently executing the character as a command (@pxref{Searching}).
562If this variable has not been given a value, the characters @key{ESC} and
563@kbd{C-J} will terminate an incremental search.
564
565@item keymap
566@vindex keymap
567Sets Readline's idea of the current keymap for key binding commands.
568Acceptable @code{keymap} names are
569@code{emacs},
570@code{emacs-standard},
571@code{emacs-meta},
572@code{emacs-ctlx},
573@code{vi},
574@code{vi-move},
575@code{vi-command}, and
576@code{vi-insert}.
577@code{vi} is equivalent to @code{vi-command}; @code{emacs} is
578equivalent to @code{emacs-standard}. The default value is @code{emacs}.
579The value of the @code{editing-mode} variable also affects the
580default keymap.
581
abe2eb5b
CR
582@item keyseq-timeout
583Specifies the duration Readline will wait for a character when reading an
584ambiguous key sequence (one that can form a complete key sequence using
585the input read so far, or can take additional input to complete a longer
586key sequence).
587If no input is received within the timeout, Readline will use the shorter
588but complete key sequence.
589The value is specified in milliseconds, so a value of 1000 means that
590Readline will wait one second for additional input.
591If this variable is set to a value less than or equal to zero, or to a
592non-numeric value, Readline will wait until another key is pressed to
593decide which key sequence to complete.
594The default value is @code{500}.
595
d3a24ed2
CR
596@item mark-directories
597If set to @samp{on}, completed directory names have a slash
598appended. The default is @samp{on}.
599
600@item mark-modified-lines
601@vindex mark-modified-lines
602This variable, when set to @samp{on}, causes Readline to display an
603asterisk (@samp{*}) at the start of history lines which have been modified.
604This variable is @samp{off} by default.
605
606@item mark-symlinked-directories
607@vindex mark-symlinked-directories
608If set to @samp{on}, completed names which are symbolic links
609to directories have a slash appended (subject to the value of
610@code{mark-directories}).
611The default is @samp{off}.
612
613@item match-hidden-files
614@vindex match-hidden-files
615This variable, when set to @samp{on}, causes Readline to match files whose
616names begin with a @samp{.} (hidden files) when performing filename
112ff2a6
CR
617completion.
618If set to @samp{off}, the leading @samp{.} must be
d3a24ed2
CR
619supplied by the user in the filename to be completed.
620This variable is @samp{on} by default.
621
d7c725b5
CR
622@item menu-complete-display-prefix
623@vindex menu-complete-display-prefix
624If set to @samp{on}, menu completion displays the common prefix of the
625list of possible completions (which may be empty) before cycling through
626the list. The default is @samp{off}.
627
d3a24ed2
CR
628@item output-meta
629@vindex output-meta
630If set to @samp{on}, Readline will display characters with the
631eighth bit set directly rather than as a meta-prefixed escape
632sequence. The default is @samp{off}.
633
634@item page-completions
635@vindex page-completions
636If set to @samp{on}, Readline uses an internal @code{more}-like pager
637to display a screenful of possible completions at a time.
638This variable is @samp{on} by default.
639
640@item print-completions-horizontally
641If set to @samp{on}, Readline will display completions with matches
642sorted horizontally in alphabetical order, rather than down the screen.
643The default is @samp{off}.
644
f13513ff
CR
645@item revert-all-at-newline
646@vindex revert-all-at-newline
647If set to @samp{on}, Readline will undo all changes to history lines
648before returning when @code{accept-line} is executed. By default,
649history lines may be modified and retain individual undo lists across
650calls to @code{readline}. The default is @samp{off}.
651
d3a24ed2
CR
652@item show-all-if-ambiguous
653@vindex show-all-if-ambiguous
654This alters the default behavior of the completion functions. If
655set to @samp{on},
656words which have more than one possible completion cause the
657matches to be listed immediately instead of ringing the bell.
658The default value is @samp{off}.
659
660@item show-all-if-unmodified
661@vindex show-all-if-unmodified
662This alters the default behavior of the completion functions in
663a fashion similar to @var{show-all-if-ambiguous}.
664If set to @samp{on},
665words which have more than one possible completion without any
666possible partial completion (the possible completions don't share
667a common prefix) cause the matches to be listed immediately instead
668of ringing the bell.
669The default value is @samp{off}.
670
a7ad477f
CR
671@item show-mode-in-prompt
672@vindex show-mode-in-prompt
673If set to @samp{on}, add a character to the beginning of the prompt
674indicating the editing mode: emacs (@samp{@@}), vi command (@samp{:}),
675or vi insertion (@samp{+}).
676The default value is @samp{off}.
677
8f714a7c
CR
678@item skip-completed-text
679@vindex skip-completed-text
680If set to @samp{on}, this alters the default completion behavior when
681inserting a single match into the line. It's only active when
682performing completion in the middle of a word. If enabled, readline
683does not insert characters from the completion that match characters
684after point in the word being completed, so portions of the word
685following the cursor are not duplicated.
686For instance, if this is enabled, attempting completion when the cursor
687is after the @samp{e} in @samp{Makefile} will result in @samp{Makefile}
688rather than @samp{Makefilefile}, assuming there is a single possible
689completion.
690The default value is @samp{off}.
691
d3a24ed2
CR
692@item visible-stats
693@vindex visible-stats
694If set to @samp{on}, a character denoting a file's type
695is appended to the filename when listing possible
696completions. The default is @samp{off}.
697
698@end table
699
700@item Key Bindings
701The syntax for controlling key bindings in the init file is
702simple. First you need to find the name of the command that you
703want to change. The following sections contain tables of the command
704name, the default keybinding, if any, and a short description of what
705the command does.
706
707Once you know the name of the command, simply place on a line
708in the init file the name of the key
709you wish to bind the command to, a colon, and then the name of the
d3ad40de
CR
710command.
711There can be no space between the key name and the colon -- that will be
712interpreted as part of the key name.
713The name of the key can be expressed in different ways, depending on
714what you find most comfortable.
d3a24ed2
CR
715
716In addition to command names, readline allows keys to be bound
717to a string that is inserted when the key is pressed (a @var{macro}).
718
719@ifset BashFeatures
720The @w{@code{bind -p}} command displays Readline function names and
721bindings in a format that can put directly into an initialization file.
722@xref{Bash Builtins}.
723@end ifset
724
725@table @asis
726@item @w{@var{keyname}: @var{function-name} or @var{macro}}
727@var{keyname} is the name of a key spelled out in English. For example:
728@example
729Control-u: universal-argument
730Meta-Rubout: backward-kill-word
731Control-o: "> output"
732@end example
733
734In the above example, @kbd{C-u} is bound to the function
735@code{universal-argument},
736@kbd{M-DEL} is bound to the function @code{backward-kill-word}, and
737@kbd{C-o} is bound to run the macro
738expressed on the right hand side (that is, to insert the text
739@samp{> output} into the line).
740
741A number of symbolic character names are recognized while
742processing this key binding syntax:
743@var{DEL},
744@var{ESC},
745@var{ESCAPE},
746@var{LFD},
747@var{NEWLINE},
748@var{RET},
749@var{RETURN},
750@var{RUBOUT},
751@var{SPACE},
752@var{SPC},
753and
754@var{TAB}.
755
756@item @w{"@var{keyseq}": @var{function-name} or @var{macro}}
757@var{keyseq} differs from @var{keyname} above in that strings
758denoting an entire key sequence can be specified, by placing
759the key sequence in double quotes. Some @sc{gnu} Emacs style key
760escapes can be used, as in the following example, but the
761special character names are not recognized.
762
763@example
764"\C-u": universal-argument
765"\C-x\C-r": re-read-init-file
766"\e[11~": "Function Key 1"
767@end example
768
769In the above example, @kbd{C-u} is again bound to the function
770@code{universal-argument} (just as it was in the first example),
771@samp{@kbd{C-x} @kbd{C-r}} is bound to the function @code{re-read-init-file},
772and @samp{@key{ESC} @key{[} @key{1} @key{1} @key{~}} is bound to insert
773the text @samp{Function Key 1}.
774
775@end table
776
777The following @sc{gnu} Emacs style escape sequences are available when
778specifying key sequences:
779
780@table @code
781@item @kbd{\C-}
782control prefix
783@item @kbd{\M-}
784meta prefix
785@item @kbd{\e}
786an escape character
787@item @kbd{\\}
788backslash
789@item @kbd{\"}
790@key{"}, a double quotation mark
791@item @kbd{\'}
792@key{'}, a single quote or apostrophe
793@end table
794
795In addition to the @sc{gnu} Emacs style escape sequences, a second
796set of backslash escapes is available:
797
798@table @code
799@item \a
800alert (bell)
801@item \b
802backspace
803@item \d
804delete
805@item \f
806form feed
807@item \n
808newline
809@item \r
810carriage return
811@item \t
812horizontal tab
813@item \v
814vertical tab
815@item \@var{nnn}
816the eight-bit character whose value is the octal value @var{nnn}
817(one to three digits)
818@item \x@var{HH}
819the eight-bit character whose value is the hexadecimal value @var{HH}
820(one or two hex digits)
821@end table
822
823When entering the text of a macro, single or double quotes must
824be used to indicate a macro definition.
825Unquoted text is assumed to be a function name.
826In the macro body, the backslash escapes described above are expanded.
827Backslash will quote any other character in the macro text,
828including @samp{"} and @samp{'}.
829For example, the following binding will make @samp{@kbd{C-x} \}
830insert a single @samp{\} into the line:
831@example
832"\C-x\\": "\\"
833@end example
834
835@end table
836
837@node Conditional Init Constructs
838@subsection Conditional Init Constructs
839
840Readline implements a facility similar in spirit to the conditional
841compilation features of the C preprocessor which allows key
842bindings and variable settings to be performed as the result
843of tests. There are four parser directives used.
844
845@table @code
846@item $if
847The @code{$if} construct allows bindings to be made based on the
848editing mode, the terminal being used, or the application using
849Readline. The text of the test extends to the end of the line;
850no characters are required to isolate it.
851
852@table @code
853@item mode
854The @code{mode=} form of the @code{$if} directive is used to test
855whether Readline is in @code{emacs} or @code{vi} mode.
856This may be used in conjunction
857with the @samp{set keymap} command, for instance, to set bindings in
858the @code{emacs-standard} and @code{emacs-ctlx} keymaps only if
859Readline is starting out in @code{emacs} mode.
860
861@item term
862The @code{term=} form may be used to include terminal-specific
863key bindings, perhaps to bind the key sequences output by the
864terminal's function keys. The word on the right side of the
865@samp{=} is tested against both the full name of the terminal and
866the portion of the terminal name before the first @samp{-}. This
867allows @code{sun} to match both @code{sun} and @code{sun-cmd},
868for instance.
869
870@item application
871The @var{application} construct is used to include
872application-specific settings. Each program using the Readline
873library sets the @var{application name}, and you can test for
874a particular value.
875This could be used to bind key sequences to functions useful for
876a specific program. For instance, the following command adds a
877key sequence that quotes the current or previous word in Bash:
878@example
879$if Bash
880# Quote the current or previous word
881"\C-xq": "\eb\"\ef\""
882$endif
883@end example
884@end table
885
886@item $endif
887This command, as seen in the previous example, terminates an
888@code{$if} command.
889
890@item $else
891Commands in this branch of the @code{$if} directive are executed if
892the test fails.
893
894@item $include
895This directive takes a single filename as an argument and reads commands
896and bindings from that file.
897For example, the following directive reads from @file{/etc/inputrc}:
898@example
899$include /etc/inputrc
900@end example
901@end table
902
903@node Sample Init File
904@subsection Sample Init File
905
906Here is an example of an @var{inputrc} file. This illustrates key
907binding, variable assignment, and conditional syntax.
908
909@example
910@page
911# This file controls the behaviour of line input editing for
912# programs that use the GNU Readline library. Existing
913# programs include FTP, Bash, and GDB.
914#
915# You can re-read the inputrc file with C-x C-r.
916# Lines beginning with '#' are comments.
917#
8581f42d 918# First, include any system-wide bindings and variable
d3a24ed2
CR
919# assignments from /etc/Inputrc
920$include /etc/Inputrc
921
922#
923# Set various bindings for emacs mode.
924
925set editing-mode emacs
926
927$if mode=emacs
928
929Meta-Control-h: backward-kill-word Text after the function name is ignored
930
931#
932# Arrow keys in keypad mode
933#
934#"\M-OD": backward-char
935#"\M-OC": forward-char
936#"\M-OA": previous-history
937#"\M-OB": next-history
938#
939# Arrow keys in ANSI mode
940#
941"\M-[D": backward-char
942"\M-[C": forward-char
943"\M-[A": previous-history
944"\M-[B": next-history
945#
946# Arrow keys in 8 bit keypad mode
947#
948#"\M-\C-OD": backward-char
949#"\M-\C-OC": forward-char
950#"\M-\C-OA": previous-history
951#"\M-\C-OB": next-history
952#
953# Arrow keys in 8 bit ANSI mode
954#
955#"\M-\C-[D": backward-char
956#"\M-\C-[C": forward-char
957#"\M-\C-[A": previous-history
958#"\M-\C-[B": next-history
959
960C-q: quoted-insert
961
962$endif
963
964# An old-style binding. This happens to be the default.
965TAB: complete
966
967# Macros that are convenient for shell interaction
968$if Bash
969# edit the path
970"\C-xp": "PATH=$@{PATH@}\e\C-e\C-a\ef\C-f"
971# prepare to type a quoted word --
972# insert open and close double quotes
973# and move to just after the open quote
974"\C-x\"": "\"\"\C-b"
975# insert a backslash (testing backslash escapes
976# in sequences and macros)
977"\C-x\\": "\\"
978# Quote the current or previous word
979"\C-xq": "\eb\"\ef\""
980# Add a binding to refresh the line, which is unbound
981"\C-xr": redraw-current-line
982# Edit variable on current line.
983"\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y="
984$endif
985
986# use a visible bell if one is available
987set bell-style visible
988
989# don't strip characters to 7 bits when reading
990set input-meta on
991
992# allow iso-latin1 characters to be inserted rather
993# than converted to prefix-meta sequences
994set convert-meta off
995
996# display characters with the eighth bit set directly
997# rather than as meta-prefixed characters
998set output-meta on
999
1000# if there are more than 150 possible completions for
1001# a word, ask the user if he wants to see all of them
1002set completion-query-items 150
1003
1004# For FTP
1005$if Ftp
1006"\C-xg": "get \M-?"
1007"\C-xt": "put \M-?"
1008"\M-.": yank-last-arg
1009$endif
1010@end example
1011
1012@node Bindable Readline Commands
1013@section Bindable Readline Commands
1014
1015@menu
1016* Commands For Moving:: Moving about the line.
1017* Commands For History:: Getting at previous lines.
1018* Commands For Text:: Commands for changing text.
1019* Commands For Killing:: Commands for killing and yanking.
1020* Numeric Arguments:: Specifying numeric arguments, repeat counts.
1021* Commands For Completion:: Getting Readline to do the typing for you.
1022* Keyboard Macros:: Saving and re-executing typed characters
1023* Miscellaneous Commands:: Other miscellaneous commands.
1024@end menu
1025
1026This section describes Readline commands that may be bound to key
1027sequences.
1028@ifset BashFeatures
1029You can list your key bindings by executing
1030@w{@code{bind -P}} or, for a more terse format, suitable for an
1031@var{inputrc} file, @w{@code{bind -p}}. (@xref{Bash Builtins}.)
1032@end ifset
1033Command names without an accompanying key sequence are unbound by default.
1034
1035In the following descriptions, @dfn{point} refers to the current cursor
1036position, and @dfn{mark} refers to a cursor position saved by the
1037@code{set-mark} command.
1038The text between the point and mark is referred to as the @dfn{region}.
1039
1040@node Commands For Moving
1041@subsection Commands For Moving
1042@ftable @code
1043@item beginning-of-line (C-a)
1044Move to the start of the current line.
1045
1046@item end-of-line (C-e)
1047Move to the end of the line.
1048
1049@item forward-char (C-f)
1050Move forward a character.
1051
1052@item backward-char (C-b)
1053Move back a character.
1054
1055@item forward-word (M-f)
8c2fef19
CR
1056Move forward to the end of the next word.
1057Words are composed of letters and digits.
d3a24ed2
CR
1058
1059@item backward-word (M-b)
8c2fef19
CR
1060Move back to the start of the current or previous word.
1061Words are composed of letters and digits.
1062
1063@ifset BashFeatures
1064@item shell-forward-word ()
1065Move forward to the end of the next word.
1066Words are delimited by non-quoted shell metacharacters.
1067
1068@item shell-backward-word ()
1069Move back to the start of the current or previous word.
1070Words are delimited by non-quoted shell metacharacters.
1071@end ifset
d3a24ed2
CR
1072
1073@item clear-screen (C-l)
1074Clear the screen and redraw the current line,
1075leaving the current line at the top of the screen.
1076
1077@item redraw-current-line ()
1078Refresh the current line. By default, this is unbound.
1079
1080@end ftable
1081
1082@node Commands For History
1083@subsection Commands For Manipulating The History
1084
1085@ftable @code
1086@item accept-line (Newline or Return)
1087@ifset BashFeatures
1088Accept the line regardless of where the cursor is.
1089If this line is
1090non-empty, add it to the history list according to the setting of
1091the @env{HISTCONTROL} and @env{HISTIGNORE} variables.
1092If this line is a modified history line, then restore the history line
1093to its original state.
1094@end ifset
1095@ifclear BashFeatures
1096Accept the line regardless of where the cursor is.
1097If this line is
1098non-empty, it may be added to the history list for future recall with
1099@code{add_history()}.
1100If this line is a modified history line, the history line is restored
1101to its original state.
1102@end ifclear
1103
1104@item previous-history (C-p)
1105Move `back' through the history list, fetching the previous command.
1106
1107@item next-history (C-n)
1108Move `forward' through the history list, fetching the next command.
1109
1110@item beginning-of-history (M-<)
1111Move to the first line in the history.
1112
1113@item end-of-history (M->)
1114Move to the end of the input history, i.e., the line currently
1115being entered.
1116
1117@item reverse-search-history (C-r)
1118Search backward starting at the current line and moving `up' through
1119the history as necessary. This is an incremental search.
1120
1121@item forward-search-history (C-s)
1122Search forward starting at the current line and moving `down' through
1123the the history as necessary. This is an incremental search.
1124
1125@item non-incremental-reverse-search-history (M-p)
1126Search backward starting at the current line and moving `up'
1127through the history as necessary using a non-incremental search
1128for a string supplied by the user.
1129
1130@item non-incremental-forward-search-history (M-n)
1131Search forward starting at the current line and moving `down'
1132through the the history as necessary using a non-incremental search
1133for a string supplied by the user.
1134
1135@item history-search-forward ()
1136Search forward through the history for the string of characters
1137between the start of the current line and the point.
276cb932 1138The search string must match at the beginning of a history line.
d3a24ed2
CR
1139This is a non-incremental search.
1140By default, this command is unbound.
1141
1142@item history-search-backward ()
1143Search backward through the history for the string of characters
276cb932
CR
1144between the start of the current line and the point.
1145The search string must match at the beginning of a history line.
1146This is a non-incremental search.
1147By default, this command is unbound.
1148
1149@item history-substr-search-forward ()
1150Search forward through the history for the string of characters
1151between the start of the current line and the point.
1152The search string may match anywhere in a history line.
1153This is a non-incremental search.
1154By default, this command is unbound.
1155
1156@item history-substr-search-backward ()
1157Search backward through the history for the string of characters
1158between the start of the current line and the point.
1159The search string may match anywhere in a history line.
1160This is a non-incremental search.
1161By default, this command is unbound.
d3a24ed2
CR
1162
1163@item yank-nth-arg (M-C-y)
1164Insert the first argument to the previous command (usually
1165the second word on the previous line) at point.
1166With an argument @var{n},
1167insert the @var{n}th word from the previous command (the words
1168in the previous command begin with word 0). A negative argument
1169inserts the @var{n}th word from the end of the previous command.
eb2bb562
CR
1170Once the argument @var{n} is computed, the argument is extracted
1171as if the @samp{!@var{n}} history expansion had been specified.
d3a24ed2
CR
1172
1173@item yank-last-arg (M-. or M-_)
1174Insert last argument to the previous command (the last word of the
adc6cff5
CR
1175previous history entry).
1176With a numeric argument, behave exactly like @code{yank-nth-arg}.
d3a24ed2 1177Successive calls to @code{yank-last-arg} move back through the history
adc6cff5
CR
1178list, inserting the last word (or the word specified by the argument to
1179the first call) of each line in turn.
1180Any numeric argument supplied to these successive calls determines
1181the direction to move through the history. A negative argument switches
1182the direction through the history (back or forward).
eb2bb562
CR
1183The history expansion facilities are used to extract the last argument,
1184as if the @samp{!$} history expansion had been specified.
d3a24ed2
CR
1185
1186@end ftable
1187
1188@node Commands For Text
1189@subsection Commands For Changing Text
1190
1191@ftable @code
b6e23235
CR
1192
1193@item @i{end-of-file} (usually C-d)
1194The character indicating end-of-file as set, for example, by
1195@code{stty}. If this character is read when there are no characters
1196on the line, and point is at the beginning of the line, Readline
1197interprets it as the end of input and returns @sc{eof}.
1198
d3a24ed2 1199@item delete-char (C-d)
b6e23235
CR
1200Delete the character at point. If this function is bound to the
1201same character as the tty @sc{eof} character, as @kbd{C-d}
1202commonly is, see above for the effects.
d3a24ed2
CR
1203
1204@item backward-delete-char (Rubout)
1205Delete the character behind the cursor. A numeric argument means
1206to kill the characters instead of deleting them.
1207
1208@item forward-backward-delete-char ()
1209Delete the character under the cursor, unless the cursor is at the
1210end of the line, in which case the character behind the cursor is
1211deleted. By default, this is not bound to a key.
1212
1213@item quoted-insert (C-q or C-v)
1214Add the next character typed to the line verbatim. This is
1215how to insert key sequences like @kbd{C-q}, for example.
1216
1217@ifclear BashFeatures
1218@item tab-insert (M-@key{TAB})
1219Insert a tab character.
1220@end ifclear
1221
1222@item self-insert (a, b, A, 1, !, @dots{})
1223Insert yourself.
1224
1225@item transpose-chars (C-t)
1226Drag the character before the cursor forward over
1227the character at the cursor, moving the
1228cursor forward as well. If the insertion point
1229is at the end of the line, then this
1230transposes the last two characters of the line.
1231Negative arguments have no effect.
1232
1233@item transpose-words (M-t)
1234Drag the word before point past the word after point,
1235moving point past that word as well.
1236If the insertion point is at the end of the line, this transposes
1237the last two words on the line.
1238
1239@item upcase-word (M-u)
1240Uppercase the current (or following) word. With a negative argument,
1241uppercase the previous word, but do not move the cursor.
1242
1243@item downcase-word (M-l)
1244Lowercase the current (or following) word. With a negative argument,
1245lowercase the previous word, but do not move the cursor.
1246
1247@item capitalize-word (M-c)
1248Capitalize the current (or following) word. With a negative argument,
1249capitalize the previous word, but do not move the cursor.
1250
1251@item overwrite-mode ()
1252Toggle overwrite mode. With an explicit positive numeric argument,
1253switches to overwrite mode. With an explicit non-positive numeric
1254argument, switches to insert mode. This command affects only
1255@code{emacs} mode; @code{vi} mode does overwrite differently.
1256Each call to @code{readline()} starts in insert mode.
1257
1258In overwrite mode, characters bound to @code{self-insert} replace
1259the text at point rather than pushing the text to the right.
1260Characters bound to @code{backward-delete-char} replace the character
1261before point with a space.
1262
1263By default, this command is unbound.
1264
1265@end ftable
1266
1267@node Commands For Killing
1268@subsection Killing And Yanking
1269
1270@ftable @code
1271
1272@item kill-line (C-k)
1273Kill the text from point to the end of the line.
1274
1275@item backward-kill-line (C-x Rubout)
1276Kill backward to the beginning of the line.
1277
1278@item unix-line-discard (C-u)
1279Kill backward from the cursor to the beginning of the current line.
1280
1281@item kill-whole-line ()
1282Kill all characters on the current line, no matter where point is.
1283By default, this is unbound.
1284
1285@item kill-word (M-d)
1286Kill from point to the end of the current word, or if between
1287words, to the end of the next word.
1288Word boundaries are the same as @code{forward-word}.
1289
1290@item backward-kill-word (M-@key{DEL})
1291Kill the word behind point.
1292Word boundaries are the same as @code{backward-word}.
1293
8c2fef19
CR
1294@ifset BashFeatures
1295@item shell-kill-word ()
1296Kill from point to the end of the current word, or if between
1297words, to the end of the next word.
1298Word boundaries are the same as @code{shell-forward-word}.
1299
e05be32d 1300@item shell-backward-kill-word ()
8c2fef19
CR
1301Kill the word behind point.
1302Word boundaries are the same as @code{shell-backward-word}.
1303@end ifset
1304
d3a24ed2
CR
1305@item unix-word-rubout (C-w)
1306Kill the word behind point, using white space as a word boundary.
1307The killed text is saved on the kill-ring.
1308
113d85a4
CR
1309@item unix-filename-rubout ()
1310Kill the word behind point, using white space and the slash character
1311as the word boundaries.
1312The killed text is saved on the kill-ring.
1313
d3a24ed2
CR
1314@item delete-horizontal-space ()
1315Delete all spaces and tabs around point. By default, this is unbound.
1316
1317@item kill-region ()
1318Kill the text in the current region.
1319By default, this command is unbound.
1320
1321@item copy-region-as-kill ()
1322Copy the text in the region to the kill buffer, so it can be yanked
1323right away. By default, this command is unbound.
1324
1325@item copy-backward-word ()
1326Copy the word before point to the kill buffer.
1327The word boundaries are the same as @code{backward-word}.
1328By default, this command is unbound.
1329
1330@item copy-forward-word ()
1331Copy the word following point to the kill buffer.
1332The word boundaries are the same as @code{forward-word}.
1333By default, this command is unbound.
1334
1335@item yank (C-y)
1336Yank the top of the kill ring into the buffer at point.
1337
1338@item yank-pop (M-y)
1339Rotate the kill-ring, and yank the new top. You can only do this if
1340the prior command is @code{yank} or @code{yank-pop}.
1341@end ftable
1342
1343@node Numeric Arguments
1344@subsection Specifying Numeric Arguments
1345@ftable @code
1346
1347@item digit-argument (@kbd{M-0}, @kbd{M-1}, @dots{} @kbd{M--})
1348Add this digit to the argument already accumulating, or start a new
1349argument. @kbd{M--} starts a negative argument.
1350
1351@item universal-argument ()
1352This is another way to specify an argument.
1353If this command is followed by one or more digits, optionally with a
1354leading minus sign, those digits define the argument.
1355If the command is followed by digits, executing @code{universal-argument}
1356again ends the numeric argument, but is otherwise ignored.
1357As a special case, if this command is immediately followed by a
1358character that is neither a digit or minus sign, the argument count
1359for the next command is multiplied by four.
1360The argument count is initially one, so executing this function the
1361first time makes the argument count four, a second time makes the
1362argument count sixteen, and so on.
1363By default, this is not bound to a key.
1364@end ftable
1365
1366@node Commands For Completion
1367@subsection Letting Readline Type For You
1368
1369@ftable @code
1370@item complete (@key{TAB})
1371Attempt to perform completion on the text before point.
1372The actual completion performed is application-specific.
1373@ifset BashFeatures
1374Bash attempts completion treating the text as a variable (if the
1375text begins with @samp{$}), username (if the text begins with
1376@samp{~}), hostname (if the text begins with @samp{@@}), or
1377command (including aliases and functions) in turn. If none
1378of these produces a match, filename completion is attempted.
1379@end ifset
1380@ifclear BashFeatures
1381The default is filename completion.
1382@end ifclear
1383
1384@item possible-completions (M-?)
1385List the possible completions of the text before point.
e502b4e0
CR
1386When displaying completions, Readline sets the number of columns used
1387for display to the value of @code{completion-display-width}, the value of
1388the environment variable @env{COLUMNS}, or the screen width, in that order.
d3a24ed2
CR
1389
1390@item insert-completions (M-*)
1391Insert all completions of the text before point that would have
1392been generated by @code{possible-completions}.
1393
1394@item menu-complete ()
1395Similar to @code{complete}, but replaces the word to be completed
1396with a single match from the list of possible completions.
1397Repeated execution of @code{menu-complete} steps through the list
1398of possible completions, inserting each match in turn.
1399At the end of the list of completions, the bell is rung
1400(subject to the setting of @code{bell-style})
1401and the original text is restored.
1402An argument of @var{n} moves @var{n} positions forward in the list
1403of matches; a negative argument may be used to move backward
1404through the list.
1405This command is intended to be bound to @key{TAB}, but is unbound
1406by default.
1407
9dd88db7
CR
1408@item menu-complete-backward ()
1409Identical to @code{menu-complete}, but moves backward through the list
1410of possible completions, as if @code{menu-complete} had been given a
1411negative argument.
1412
d3a24ed2
CR
1413@item delete-char-or-list ()
1414Deletes the character under the cursor if not at the beginning or
1415end of the line (like @code{delete-char}).
1416If at the end of the line, behaves identically to
1417@code{possible-completions}.
1418This command is unbound by default.
1419
1420@ifset BashFeatures
1421@item complete-filename (M-/)
1422Attempt filename completion on the text before point.
1423
1424@item possible-filename-completions (C-x /)
1425List the possible completions of the text before point,
1426treating it as a filename.
1427
1428@item complete-username (M-~)
1429Attempt completion on the text before point, treating
1430it as a username.
1431
1432@item possible-username-completions (C-x ~)
1433List the possible completions of the text before point,
1434treating it as a username.
1435
1436@item complete-variable (M-$)
1437Attempt completion on the text before point, treating
1438it as a shell variable.
1439
1440@item possible-variable-completions (C-x $)
1441List the possible completions of the text before point,
1442treating it as a shell variable.
1443
1444@item complete-hostname (M-@@)
1445Attempt completion on the text before point, treating
1446it as a hostname.
1447
1448@item possible-hostname-completions (C-x @@)
1449List the possible completions of the text before point,
1450treating it as a hostname.
1451
1452@item complete-command (M-!)
1453Attempt completion on the text before point, treating
1454it as a command name. Command completion attempts to
1455match the text against aliases, reserved words, shell
1456functions, shell builtins, and finally executable filenames,
1457in that order.
1458
1459@item possible-command-completions (C-x !)
1460List the possible completions of the text before point,
1461treating it as a command name.
1462
1463@item dynamic-complete-history (M-@key{TAB})
1464Attempt completion on the text before point, comparing
1465the text against lines from the history list for possible
1466completion matches.
1467
8943768b
CR
1468@item dabbrev-expand ()
1469Attempt menu completion on the text before point, comparing
1470the text against lines from the history list for possible
1471completion matches.
1472
d3a24ed2
CR
1473@item complete-into-braces (M-@{)
1474Perform filename completion and insert the list of possible completions
1475enclosed within braces so the list is available to the shell
1476(@pxref{Brace Expansion}).
1477
1478@end ifset
1479@end ftable
1480
1481@node Keyboard Macros
1482@subsection Keyboard Macros
1483@ftable @code
1484
1485@item start-kbd-macro (C-x ()
1486Begin saving the characters typed into the current keyboard macro.
1487
1488@item end-kbd-macro (C-x ))
1489Stop saving the characters typed into the current keyboard macro
1490and save the definition.
1491
1492@item call-last-kbd-macro (C-x e)
1493Re-execute the last keyboard macro defined, by making the characters
1494in the macro appear as if typed at the keyboard.
1495
ba4ab055
CR
1496@item print-last-kbd-macro ()
1497Print the last keboard macro defined in a format suitable for the
1498@var{inputrc} file.
1499
d3a24ed2
CR
1500@end ftable
1501
1502@node Miscellaneous Commands
1503@subsection Some Miscellaneous Commands
1504@ftable @code
1505
1506@item re-read-init-file (C-x C-r)
1507Read in the contents of the @var{inputrc} file, and incorporate
1508any bindings or variable assignments found there.
1509
1510@item abort (C-g)
1511Abort the current editing command and
1512ring the terminal's bell (subject to the setting of
1513@code{bell-style}).
1514
1515@item do-uppercase-version (M-a, M-b, M-@var{x}, @dots{})
1516If the metafied character @var{x} is lowercase, run the command
1517that is bound to the corresponding uppercase character.
1518
1519@item prefix-meta (@key{ESC})
1520Metafy the next character typed. This is for keyboards
1521without a meta key. Typing @samp{@key{ESC} f} is equivalent to typing
1522@kbd{M-f}.
1523
1524@item undo (C-_ or C-x C-u)
1525Incremental undo, separately remembered for each line.
1526
1527@item revert-line (M-r)
1528Undo all changes made to this line. This is like executing the @code{undo}
1529command enough times to get back to the beginning.
1530
1531@ifset BashFeatures
1532@item tilde-expand (M-&)
1533@end ifset
1534@ifclear BashFeatures
1535@item tilde-expand (M-~)
1536@end ifclear
1537Perform tilde expansion on the current word.
1538
1539@item set-mark (C-@@)
1540Set the mark to the point. If a
1541numeric argument is supplied, the mark is set to that position.
1542
1543@item exchange-point-and-mark (C-x C-x)
1544Swap the point with the mark. The current cursor position is set to
1545the saved position, and the old cursor position is saved as the mark.
1546
1547@item character-search (C-])
1548A character is read and point is moved to the next occurrence of that
1549character. A negative count searches for previous occurrences.
1550
1551@item character-search-backward (M-C-])
1552A character is read and point is moved to the previous occurrence
1553of that character. A negative count searches for subsequent
1554occurrences.
1555
691aebcb
CR
1556@item skip-csi-sequence ()
1557Read enough characters to consume a multi-key sequence such as those
1558defined for keys like Home and End. Such sequences begin with a
1559Control Sequence Indicator (CSI), usually ESC-[. If this sequence is
1560bound to "\e[", keys producing such sequences will have no effect
1561unless explicitly bound to a readline command, instead of inserting
1562stray characters into the editing buffer. This is unbound by default,
1563but usually bound to ESC-[.
1564
d3a24ed2
CR
1565@item insert-comment (M-#)
1566Without a numeric argument, the value of the @code{comment-begin}
1567variable is inserted at the beginning of the current line.
1568If a numeric argument is supplied, this command acts as a toggle: if
1569the characters at the beginning of the line do not match the value
1570of @code{comment-begin}, the value is inserted, otherwise
1571the characters in @code{comment-begin} are deleted from the beginning of
1572the line.
1573In either case, the line is accepted as if a newline had been typed.
1574@ifset BashFeatures
1575The default value of @code{comment-begin} causes this command
1576to make the current line a shell comment.
1577If a numeric argument causes the comment character to be removed, the line
1578will be executed by the shell.
1579@end ifset
1580
1581@item dump-functions ()
1582Print all of the functions and their key bindings to the
1583Readline output stream. If a numeric argument is supplied,
1584the output is formatted in such a way that it can be made part
1585of an @var{inputrc} file. This command is unbound by default.
1586
1587@item dump-variables ()
1588Print all of the settable variables and their values to the
1589Readline output stream. If a numeric argument is supplied,
1590the output is formatted in such a way that it can be made part
1591of an @var{inputrc} file. This command is unbound by default.
1592
1593@item dump-macros ()
1594Print all of the Readline key sequences bound to macros and the
1595strings they output. If a numeric argument is supplied,
1596the output is formatted in such a way that it can be made part
1597of an @var{inputrc} file. This command is unbound by default.
1598
1599@ifset BashFeatures
1600@item glob-complete-word (M-g)
1601The word before point is treated as a pattern for pathname expansion,
1602with an asterisk implicitly appended. This pattern is used to
1603generate a list of matching file names for possible completions.
1604
1605@item glob-expand-word (C-x *)
1606The word before point is treated as a pattern for pathname expansion,
1607and the list of matching file names is inserted, replacing the word.
1608If a numeric argument is supplied, a @samp{*} is appended before
1609pathname expansion.
1610
1611@item glob-list-expansions (C-x g)
1612The list of expansions that would have been generated by
1613@code{glob-expand-word} is displayed, and the line is redrawn.
1614If a numeric argument is supplied, a @samp{*} is appended before
1615pathname expansion.
1616
1617@item display-shell-version (C-x C-v)
1618Display version information about the current instance of Bash.
1619
1620@item shell-expand-line (M-C-e)
1621Expand the line as the shell does.
1622This performs alias and history expansion as well as all of the shell
1623word expansions (@pxref{Shell Expansions}).
1624
1625@item history-expand-line (M-^)
1626Perform history expansion on the current line.
1627
1628@item magic-space ()
1629Perform history expansion on the current line and insert a space
1630(@pxref{History Interaction}).
1631
1632@item alias-expand-line ()
1633Perform alias expansion on the current line (@pxref{Aliases}).
1634
1635@item history-and-alias-expand-line ()
1636Perform history and alias expansion on the current line.
1637
1638@item insert-last-argument (M-. or M-_)
1639A synonym for @code{yank-last-arg}.
1640
1641@item operate-and-get-next (C-o)
1642Accept the current line for execution and fetch the next line
1643relative to the current line from the history for editing. Any
1644argument is ignored.
1645
1646@item edit-and-execute-command (C-xC-e)
1647Invoke an editor on the current command line, and execute the result as shell
1648commands.
1649Bash attempts to invoke
de8913bd 1650@code{$VISUAL}, @code{$EDITOR}, and @code{emacs}
d3a24ed2
CR
1651as the editor, in that order.
1652
1653@end ifset
1654
1655@ifclear BashFeatures
1656@item emacs-editing-mode (C-e)
1657When in @code{vi} command mode, this causes a switch to @code{emacs}
1658editing mode.
1659
1660@item vi-editing-mode (M-C-j)
1661When in @code{emacs} editing mode, this causes a switch to @code{vi}
1662editing mode.
1663
1664@end ifclear
1665
1666@end ftable
1667
1668@node Readline vi Mode
1669@section Readline vi Mode
1670
1671While the Readline library does not have a full set of @code{vi}
1672editing functions, it does contain enough to allow simple editing
1673of the line. The Readline @code{vi} mode behaves as specified in
112ff2a6 1674the @sc{posix} standard.
d3a24ed2
CR
1675
1676@ifset BashFeatures
1677In order to switch interactively between @code{emacs} and @code{vi}
1678editing modes, use the @samp{set -o emacs} and @samp{set -o vi}
1679commands (@pxref{The Set Builtin}).
1680@end ifset
1681@ifclear BashFeatures
1682In order to switch interactively between @code{emacs} and @code{vi}
1683editing modes, use the command @kbd{M-C-j} (bound to emacs-editing-mode
1684when in @code{vi} mode and to vi-editing-mode in @code{emacs} mode).
1685@end ifclear
1686The Readline default is @code{emacs} mode.
1687
1688When you enter a line in @code{vi} mode, you are already placed in
1689`insertion' mode, as if you had typed an @samp{i}. Pressing @key{ESC}
1690switches you into `command' mode, where you can edit the text of the
1691line with the standard @code{vi} movement keys, move to previous
1692history lines with @samp{k} and subsequent lines with @samp{j}, and
1693so forth.
1694
1695@ifset BashFeatures
1696@node Programmable Completion
1697@section Programmable Completion
1698@cindex programmable completion
1699
1700When word completion is attempted for an argument to a command for
1701which a completion specification (a @var{compspec}) has been defined
1702using the @code{complete} builtin (@pxref{Programmable Completion Builtins}),
1703the programmable completion facilities are invoked.
1704
1705First, the command name is identified.
1706If a compspec has been defined for that command, the
1707compspec is used to generate the list of possible completions for the word.
3eb2d94a
CR
1708If the command word is the empty string (completion attempted at the
1709beginning of an empty line), any compspec defined with
1710the @option{-E} option to @code{complete} is used.
d3a24ed2
CR
1711If the command word is a full pathname, a compspec for the full
1712pathname is searched for first.
1713If no compspec is found for the full pathname, an attempt is made to
1714find a compspec for the portion following the final slash.
3eb2d94a
CR
1715If those searches do not result in a compspec, any compspec defined with
1716the @option{-D} option to @code{complete} is used as the default.
d3a24ed2
CR
1717
1718Once a compspec has been found, it is used to generate the list of
1719matching words.
1720If a compspec is not found, the default Bash completion
1721described above (@pxref{Commands For Completion}) is performed.
1722
1723First, the actions specified by the compspec are used.
1724Only matches which are prefixed by the word being completed are
1725returned.
1726When the @option{-f} or @option{-d} option is used for filename or
1727directory name completion, the shell variable @env{FIGNORE} is
1728used to filter the matches.
1729@xref{Bash Variables}, for a description of @env{FIGNORE}.
1730
1731Any completions specified by a filename expansion pattern to the
1732@option{-G} option are generated next.
1733The words generated by the pattern need not match the word being completed.
1734The @env{GLOBIGNORE} shell variable is not used to filter the matches,
1735but the @env{FIGNORE} shell variable is used.
1736
1737Next, the string specified as the argument to the @option{-W} option
1738is considered.
1739The string is first split using the characters in the @env{IFS}
1740special variable as delimiters.
1741Shell quoting is honored.
1742Each word is then expanded using
1743brace expansion, tilde expansion, parameter and variable expansion,
6e70dbff 1744command substitution, and arithmetic expansion,
d3a24ed2
CR
1745as described above (@pxref{Shell Expansions}).
1746The results are split using the rules described above
1747(@pxref{Word Splitting}).
1748The results of the expansion are prefix-matched against the word being
1749completed, and the matching words become the possible completions.
1750
1751After these matches have been generated, any shell function or command
1752specified with the @option{-F} and @option{-C} options is invoked.
d3ad40de
CR
1753When the command or function is invoked, the @env{COMP_LINE},
1754@env{COMP_POINT}, @env{COMP_KEY}, and @env{COMP_TYPE} variables are
1755assigned values as described above (@pxref{Bash Variables}).
d3a24ed2
CR
1756If a shell function is being invoked, the @env{COMP_WORDS} and
1757@env{COMP_CWORD} variables are also set.
eb4206df 1758When the function or command is invoked, the first argument ($1) is the
d3a24ed2 1759name of the command whose arguments are being completed, the
eb4206df
CR
1760second argument ($2) is the word being completed, and the third argument
1761($3) is the word preceding the word being completed on the current command
1762line.
d3a24ed2
CR
1763No filtering of the generated completions against the word being completed
1764is performed; the function or command has complete freedom in generating
1765the matches.
1766
1767Any function specified with @option{-F} is invoked first.
1768The function may use any of the shell facilities, including the
6fbe7620 1769@code{compgen} and @code{compopt} builtins described below
d3a24ed2
CR
1770(@pxref{Programmable Completion Builtins}), to generate the matches.
1771It must put the possible completions in the @env{COMPREPLY} array
eb4206df 1772variable, one per array element.
d3a24ed2
CR
1773
1774Next, any command specified with the @option{-C} option is invoked
1775in an environment equivalent to command substitution.
1776It should print a list of completions, one per line, to
1777the standard output.
1778Backslash may be used to escape a newline, if necessary.
1779
1780After all of the possible completions are generated, any filter
1781specified with the @option{-X} option is applied to the list.
1782The filter is a pattern as used for pathname expansion; a @samp{&}
1783in the pattern is replaced with the text of the word being completed.
1784A literal @samp{&} may be escaped with a backslash; the backslash
1785is removed before attempting a match.
1786Any completion that matches the pattern will be removed from the list.
1787A leading @samp{!} negates the pattern; in this case any completion
1788not matching the pattern will be removed.
1789
1790Finally, any prefix and suffix specified with the @option{-P} and @option{-S}
1791options are added to each member of the completion list, and the result is
1792returned to the Readline completion code as the list of possible
1793completions.
1794
1795If the previously-applied actions do not generate any matches, and the
1796@option{-o dirnames} option was supplied to @code{complete} when the
1797compspec was defined, directory name completion is attempted.
1798
1799If the @option{-o plusdirs} option was supplied to @code{complete} when
1800the compspec was defined, directory name completion is attempted and any
1801matches are added to the results of the other actions.
1802
1803By default, if a compspec is found, whatever it generates is returned to
1804the completion code as the full set of possible completions.
1805The default Bash completions are not attempted, and the Readline default
1806of filename completion is disabled.
1807If the @option{-o bashdefault} option was supplied to @code{complete} when
1808the compspec was defined, the default Bash completions are attempted
1809if the compspec generates no matches.
1810If the @option{-o default} option was supplied to @code{complete} when the
1811compspec was defined, Readline's default completion will be performed
1812if the compspec (and, if attempted, the default Bash completions)
1813generate no matches.
1814
1815When a compspec indicates that directory name completion is desired,
1816the programmable completion functions force Readline to append a slash
1817to completed names which are symbolic links to directories, subject to
1818the value of the @var{mark-directories} Readline variable, regardless
1819of the setting of the @var{mark-symlinked-directories} Readline variable.
1820
3eb2d94a
CR
1821There is some support for dynamically modifying completions. This is
1822most useful when used in combination with a default completion specified
1823with @option{-D}. It's possible for shell functions executed as completion
1824handlers to indicate that completion should be retried by returning an
1825exit status of 124. If a shell function returns 124, and changes
1826the compspec associated with the command on which completion is being
1827attempted (supplied as the first argument when the function is executed),
1828programmable completion restarts from the beginning, with an
3d8cce26 1829attempt to find a new compspec for that command. This allows a set of
3eb2d94a
CR
1830completions to be built dynamically as completion is attempted, rather than
1831being loaded all at once.
1832
1833For instance, assuming that there is a library of compspecs, each kept in a
1834file corresponding to the name of the command, the following default
1835completion function would load completions dynamically:
1836
8f714a7c 1837@example
3eb2d94a 1838_completion_loader()
8f714a7c 1839@{
45c0f7f8 1840 . "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return 124
8f714a7c 1841@}
145d6930 1842complete -D -F _completion_loader -o bashdefault -o default
8f714a7c 1843@end example
3eb2d94a 1844
d3a24ed2
CR
1845@node Programmable Completion Builtins
1846@section Programmable Completion Builtins
1847@cindex completion builtins
1848
ba4ab055
CR
1849Three builtin commands are available to manipulate the programmable completion
1850facilities: one to specify how the arguments to a particular command are to
1851be completed, and two to modify the completion as it is happening.
d3a24ed2
CR
1852
1853@table @code
1854@item compgen
1855@btindex compgen
1856@example
1857@code{compgen [@var{option}] [@var{word}]}
1858@end example
1859
1860Generate possible completion matches for @var{word} according to
1861the @var{option}s, which may be any option accepted by the
1862@code{complete}
1863builtin with the exception of @option{-p} and @option{-r}, and write
1864the matches to the standard output.
1865When using the @option{-F} or @option{-C} options, the various shell variables
1866set by the programmable completion facilities, while available, will not
1867have useful values.
1868
1869The matches will be generated in the same way as if the programmable
1870completion code had generated them directly from a completion specification
1871with the same flags.
1872If @var{word} is specified, only those completions matching @var{word}
1873will be displayed.
1874
1875The return value is true unless an invalid option is supplied, or no
1876matches were generated.
1877
1878@item complete
1879@btindex complete
1880@example
3eb2d94a 1881@code{complete [-abcdefgjksuv] [-o @var{comp-option}] [-DE] [-A @var{action}] [-G @var{globpat}] [-W @var{wordlist}]
d3ad40de
CR
1882[-F @var{function}] [-C @var{command}] [-X @var{filterpat}]
1883[-P @var{prefix}] [-S @var{suffix}] @var{name} [@var{name} @dots{}]}
3eb2d94a 1884@code{complete -pr [-DE] [@var{name} @dots{}]}
d3a24ed2
CR
1885@end example
1886
1887Specify how arguments to each @var{name} should be completed.
1888If the @option{-p} option is supplied, or if no options are supplied, existing
1889completion specifications are printed in a way that allows them to be
1890reused as input.
1891The @option{-r} option removes a completion specification for
1892each @var{name}, or, if no @var{name}s are supplied, all
1893completion specifications.
3eb2d94a
CR
1894The @option{-D} option indicates that the remaining options and actions should
1895apply to the ``default'' command completion; that is, completion attempted
1896on a command for which no completion has previously been defined.
6a8fd0ed
CR
1897The @option{-E} option indicates that the remaining options and actions should
1898apply to ``empty'' command completion; that is, completion attempted on a
1899blank line.
d3a24ed2
CR
1900
1901The process of applying these completion specifications when word completion
3eb2d94a
CR
1902is attempted is described above (@pxref{Programmable Completion}). The
1903@option{-D} option takes precedence over @option{-E}.
d3a24ed2
CR
1904
1905Other options, if specified, have the following meanings.
1906The arguments to the @option{-G}, @option{-W}, and @option{-X} options
1907(and, if necessary, the @option{-P} and @option{-S} options)
1908should be quoted to protect them from expansion before the
1909@code{complete} builtin is invoked.
1910
1911
1912@table @code
1913@item -o @var{comp-option}
1914The @var{comp-option} controls several aspects of the compspec's behavior
1915beyond the simple generation of completions.
1916@var{comp-option} may be one of:
1917
1918@table @code
1919
1920@item bashdefault
1921Perform the rest of the default Bash completions if the compspec
1922generates no matches.
1923
1924@item default
1925Use Readline's default filename completion if the compspec generates
1926no matches.
1927
1928@item dirnames
1929Perform directory name completion if the compspec generates no matches.
1930
1931@item filenames
1932Tell Readline that the compspec generates filenames, so it can perform any
e77a3058
CR
1933filename-specific processing (like adding a slash to directory names
1934quoting special characters, or suppressing trailing spaces).
1935This option is intended to be used with shell functions specified
1936with @option{-F}.
d3a24ed2 1937
084c952b
CR
1938@item noquote
1939Tell Readline not to quote the completed words if they are filenames
1940(quoting filenames is the default).
1941
d3a24ed2
CR
1942@item nospace
1943Tell Readline not to append a space (the default) to words completed at
1944the end of the line.
301e2142
CR
1945
1946@item plusdirs
1947After any matches defined by the compspec are generated,
1948directory name completion is attempted and any
1949matches are added to the results of the other actions.
1950
d3a24ed2
CR
1951@end table
1952
1953@item -A @var{action}
1954The @var{action} may be one of the following to generate a list of possible
1955completions:
1956
1957@table @code
1958@item alias
1959Alias names. May also be specified as @option{-a}.
1960
1961@item arrayvar
1962Array variable names.
1963
1964@item binding
1965Readline key binding names (@pxref{Bindable Readline Commands}).
1966
1967@item builtin
1968Names of shell builtin commands. May also be specified as @option{-b}.
1969
1970@item command
1971Command names. May also be specified as @option{-c}.
1972
1973@item directory
1974Directory names. May also be specified as @option{-d}.
1975
1976@item disabled
1977Names of disabled shell builtins.
1978
1979@item enabled
1980Names of enabled shell builtins.
1981
1982@item export
1983Names of exported shell variables. May also be specified as @option{-e}.
1984
1985@item file
1986File names. May also be specified as @option{-f}.
1987
1988@item function
1989Names of shell functions.
1990
1991@item group
1992Group names. May also be specified as @option{-g}.
1993
1994@item helptopic
1995Help topics as accepted by the @code{help} builtin (@pxref{Bash Builtins}).
1996
1997@item hostname
1998Hostnames, as taken from the file specified by the
1999@env{HOSTFILE} shell variable (@pxref{Bash Variables}).
2000
2001@item job
2002Job names, if job control is active. May also be specified as @option{-j}.
2003
2004@item keyword
2005Shell reserved words. May also be specified as @option{-k}.
2006
2007@item running
2008Names of running jobs, if job control is active.
2009
2010@item service
2011Service names. May also be specified as @option{-s}.
2012
2013@item setopt
2014Valid arguments for the @option{-o} option to the @code{set} builtin
2015(@pxref{The Set Builtin}).
2016
2017@item shopt
2018Shell option names as accepted by the @code{shopt} builtin
2019(@pxref{Bash Builtins}).
2020
2021@item signal
2022Signal names.
2023
2024@item stopped
2025Names of stopped jobs, if job control is active.
2026
2027@item user
2028User names. May also be specified as @option{-u}.
2029
2030@item variable
2031Names of all shell variables. May also be specified as @option{-v}.
2032@end table
2033
d3a24ed2
CR
2034@item -C @var{command}
2035@var{command} is executed in a subshell environment, and its output is
2036used as the possible completions.
2037
2038@item -F @var{function}
2039The shell function @var{function} is executed in the current shell
2040environment.
eb4206df
CR
2041When it is executed, $1 is the name of the command whose arguments are
2042being completed, $2 is the word being completed, and $3 is the word
2043preceding the word being completed, as described above
2044(@pxref{Programmable Completion}).
d3a24ed2
CR
2045When it finishes, the possible completions are retrieved from the value
2046of the @env{COMPREPLY} array variable.
2047
5cdaaf76
CR
2048@item -G @var{globpat}
2049The filename expansion pattern @var{globpat} is expanded to generate
2050the possible completions.
d3a24ed2
CR
2051
2052@item -P @var{prefix}
2053@var{prefix} is added at the beginning of each possible completion
2054after all other options have been applied.
2055
2056@item -S @var{suffix}
2057@var{suffix} is appended to each possible completion
2058after all other options have been applied.
5cdaaf76
CR
2059
2060@item -W @var{wordlist}
2061The @var{wordlist} is split using the characters in the
2062@env{IFS} special variable as delimiters, and each resultant word
2063is expanded.
2064The possible completions are the members of the resultant list which
2065match the word being completed.
2066
2067@item -X @var{filterpat}
2068@var{filterpat} is a pattern as used for filename expansion.
2069It is applied to the list of possible completions generated by the
2070preceding options and arguments, and each completion matching
2071@var{filterpat} is removed from the list.
2072A leading @samp{!} in @var{filterpat} negates the pattern; in this
2073case, any completion not matching @var{filterpat} is removed.
d3a24ed2
CR
2074@end table
2075
2076The return value is true unless an invalid option is supplied, an option
2077other than @option{-p} or @option{-r} is supplied without a @var{name}
2078argument, an attempt is made to remove a completion specification for
2079a @var{name} for which no specification exists, or
2080an error occurs adding a completion specification.
2081
6fbe7620
CR
2082@item compopt
2083@btindex compopt
2084@example
3eb2d94a 2085@code{compopt} [-o @var{option}] [-DE] [+o @var{option}] [@var{name}]
6fbe7620
CR
2086@end example
2087Modify completion options for each @var{name} according to the
5cdaaf76 2088@var{option}s, or for the currently-executing completion if no @var{name}s
6fbe7620
CR
2089are supplied.
2090If no @var{option}s are given, display the completion options for each
2091@var{name} or the current completion.
2092The possible values of @var{option} are those valid for the @code{complete}
2093builtin described above.
3eb2d94a
CR
2094The @option{-D} option indicates that the remaining options should
2095apply to the ``default'' command completion; that is, completion attempted
2096on a command for which no completion has previously been defined.
2097The @option{-E} option indicates that the remaining options should
2098apply to ``empty'' command completion; that is, completion attempted on a
2099blank line.
2100
2101The @option{-D} option takes precedence over @option{-E}.
6fbe7620
CR
2102
2103The return value is true unless an invalid option is supplied, an attempt
2104is made to modify the options for a @var{name} for which no completion
2105specification exists, or an output error occurs.
2106
6a8fd0ed
CR
2107@end table
2108
eb4206df
CR
2109@node A Programmable Completion Example
2110@section A Programmable Completion Example
2111
45c0f7f8
CR
2112The most common way to obtain additional completion functionality beyond
2113the default actions @code{complete} and @code{compgen} provide is to use
2114a shell function and bind it to a particular command using @code{complete -F}.
2115
2116The following function provides completions for the @code{cd} builtin.
2117It is a reasonably good example of what shell functions must do when
2118used for completion. This function uses the word passsed as @code{$2}
2119to determine the directory name to complete. You can also use the
2120@code{COMP_WORDS} array variable; the current word is indexed by the
2121@code{COMP_CWORD} variable.
2122
2123The function relies on the @code{complete} and @code{compgen} builtins
2124to do much of the work, adding only the things that the Bash @code{cd}
2125does beyond accepting basic directory names:
2126tilde expansion (@pxref{Tilde Expansion}),
2127searching directories in @var{$CDPATH}, which is described above
2128(@pxref{Bourne Shell Builtins}),
2129and basic support for the @code{cdable_vars} shell option
2130(@pxref{The Shopt Builtin}).
2131@code{_comp_cd} modifies the value of @var{IFS} so that it contains only
2132a newline to accommodate file names containing spaces and tabs --
2133@code{compgen} prints the possible completions it generates one per line.
2134
2135Possible completions go into the @var{COMPREPLY} array variable, one
2136completion per array element. The programmable completion system retrieves
2137the completions from there when the function returns.
2138
eb4206df
CR
2139@example
2140# A completion function for the cd builtin
2141# based on the cd completion function from the bash_completion package
2142_comp_cd()
45c0f7f8
CR
2143@{
2144 local IFS=$' \t\n' # normalize IFS
2145 local cur _skipdot _cdpath
2146 local i j k
2147
2148 # Tilde expansion, with side effect of expanding tilde to full pathname
2149 case "$2" in
2150 \~*) eval cur="$2" ;;
2151 *) cur=$2 ;;
2152 esac
2153
2154 # no cdpath or absolute pathname -- straight directory completion
2155 if [[ -z "$@{CDPATH:-@}" ]] || [[ "$cur" == @@(./*|../*|/*) ]]; then
2156 # compgen prints paths one per line; could also use while loop
2157 IFS=$'\n'
2158 COMPREPLY=( $(compgen -d -- "$cur") )
2159 IFS=$' \t\n'
2160 # CDPATH+directories in the current directory if not in CDPATH
2161 else
2162 IFS=$'\n'
2163 _skipdot=false
2164 # preprocess CDPATH to convert null directory names to .
2165 _cdpath=$@{CDPATH/#:/.:@}
2166 _cdpath=$@{_cdpath//::/:.:@}
2167 _cdpath=$@{_cdpath/%:/:.@}
2168 for i in $@{_cdpath//:/$'\n'@}; do
2169 if [[ $i -ef . ]]; then _skipdot=true; fi
2170 k="$@{#COMPREPLY[@@]@}"
2171 for j in $( compgen -d -- "$i/$cur" ); do
2172 COMPREPLY[k++]=$@{j#$i/@} # cut off directory
2173 done
2174 done
2175 $_skipdot || COMPREPLY+=( $(compgen -d -- "$cur") )
2176 IFS=$' \t\n'
2177 fi
2178
2179 # variable names if appropriate shell option set and no completions
2180 if shopt -q cdable_vars && [[ $@{#COMPREPLY[@@]@} -eq 0 ]]; then
2181 COMPREPLY=( $(compgen -v -- "$cur") )
2182 fi
2183
2184 return 0
2185@}
2186@end example
2187
2188We install the completion function using the @option{-F} option to
2189@code{complete}:
eb4206df 2190
45c0f7f8 2191@example
eb4206df
CR
2192# Tell readline to quote appropriate and append slashes to directories;
2193# use the bash default completion for other arguments
2194complete -o filenames -o nospace -o bashdefault -F _comp_cd cd
eb4206df
CR
2195@end example
2196
45c0f7f8
CR
2197@noindent
2198Since we'd like Bash and Readline to take care of some
2199of the other details for us, we use several other options to tell Bash
2200and Readline what to do. The @option{-o filenames} option tells Readline
2201that the possible completions should be treated as filenames, and quoted
2202appropriately. That option will also cause Readline to append a slash to
2203filenames it can determine are directories (which is why we might want to
2204extend @code{_comp_cd} to append a slash if we're using directories found
2205via @var{CDPATH}: Readline can't tell those completions are directories).
2206The @option{-o nospace} option tells Readline to not append a space
2207character to the directory name, in case we want to append to it.
2208The @option{-o bashdefault} option brings in the rest of the "Bash default"
2209completions -- possible completion that Bash adds to the default Readline
2210set. These include things like command name completion, variable completion
2211for words beginning with @samp{@{}, completions containing pathname
2212expansion patterns (@pxref{Filename Expansion}), and so on.
2213
2214Once installed using @code{complete}, @code{_comp_cd} will be called every
2215time we attempt word completion for a @code{cd} command.
2216
2217Many more examples -- an extensive collection of completions for most of
2218the common GNU, Unix, and Linux commands -- are available as part of the
2219bash_completion project. This is installed by default on many GNU/Linux
2220distributions. Originally written by Ian Macdonald, the project now lives
2221at @url{http://bash-completion.alioth.debian.org/}. There are ports for
2222other systems such as Solaris and Mac OS X.
2223
2224An older version of the bash_completion package is distributed with bash
2225in the @file{examples/complete} subdirectory.
2226
d3a24ed2 2227@end ifset