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