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