Programmable Completion Builtins (Bash Reference Manual)
Next: A Programmable Completion Example, Previous: Programmable Completion, Up: Command Line Editing [Contents][Index]
8.7 Programmable Completion Builtins
Three builtin commands are available to manipulate the programmable completion facilities: one to specify how the arguments to a particular command are to be completed, and two to modify the completion as it is happening.
compgencompgen [option] [word]
Generate possible completion matches for
wordaccording to theoptions, which may be any option accepted by thecompletebuiltin with the exception of-pand-r, and write the matches to the standard output. When using the-For-Coptions, the various shell variables set by the programmable completion facilities, while available, will not have useful values.The matches will be generated in the same way as if the programmable completion code had generated them directly from a completion specification with the same flags. If
wordis specified, only those completions matchingwordwill be displayed.The return value is true unless an invalid option is supplied, or no matches were generated.
completecomplete [-abcdefgjksuv] [-o comp-option] [-DEI] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] name [name …] complete -pr [-DEI] [name …]
Specify how arguments to each
nameshould be completed. If the-poption is supplied, or if no options are supplied, existing completion specifications are printed in a way that allows them to be reused as input. The-roption removes a completion specification for eachname, or, if nonames are supplied, all completion specifications. The-Doption indicates that other supplied options and actions should apply to the “default” command completion; that is, completion attempted on a command for which no completion has previously been defined. The-Eoption indicates that other supplied options and actions should apply to “empty” command completion; that is, completion attempted on a blank line. The-Ioption indicates that other supplied options and actions should apply to completion on the initial non-assignment word on the line, or after a command delimiter such as ‘;’ or ‘|’, which is usually command name completion. If multiple options are supplied, the-Doption takes precedence over-E, and both take precedence over-I. If any of-D,-E, or-Iare supplied, any othernamearguments are ignored; these completions only apply to the case specified by the option.The process of applying these completion specifications when word completion is attempted is described above (see Programmable Completion).
Other options, if specified, have the following meanings. The arguments to the
-G,-W, and-Xoptions (and, if necessary, the-Pand-Soptions) should be quoted to protect them from expansion before thecompletebuiltin is invoked.-o comp-optionThe
comp-optioncontrols several aspects of the compspec’s behavior beyond the simple generation of completions.comp-optionmay be one of:bashdefaultPerform the rest of the default Bash completions if the compspec generates no matches.
defaultUse Readline’s default filename completion if the compspec generates no matches.
dirnamesPerform directory name completion if the compspec generates no matches.
filenamesTell Readline that the compspec generates filenames, so it can perform any filename-specific processing (like adding a slash to directory names, quoting special characters, or suppressing trailing spaces). This option is intended to be used with shell functions specified with
-F.noquoteTell Readline not to quote the completed words if they are filenames (quoting filenames is the default).
nosortTell Readline not to sort the list of possible completions alphabetically.
nospaceTell Readline not to append a space (the default) to words completed at the end of the line.
plusdirsAfter any matches defined by the compspec are generated, directory name completion is attempted and any matches are added to the results of the other actions.
-A actionThe
actionmay be one of the following to generate a list of possible completions:aliasAlias names. May also be specified as
-a.arrayvarArray variable names.
bindingReadline key binding names (see Bindable Readline Commands).
builtinNames of shell builtin commands. May also be specified as
-b.commandCommand names. May also be specified as
-c.directoryDirectory names. May also be specified as
-d.disabledNames of disabled shell builtins.
enabledNames of enabled shell builtins.
exportNames of exported shell variables. May also be specified as
-e.fileFile names. May also be specified as
-f.functionNames of shell functions.
groupGroup names. May also be specified as
-g.helptopicHelp topics as accepted by the
helpbuiltin (see Bash Builtins).hostnameHostnames, as taken from the file specified by the
HOSTFILEshell variable (see Bash Variables).jobJob names, if job control is active. May also be specified as
-j.keywordShell reserved words. May also be specified as
-k.runningNames of running jobs, if job control is active.
serviceService names. May also be specified as
-s.setoptValid arguments for the
-ooption to thesetbuiltin (see The Set Builtin).shoptShell option names as accepted by the
shoptbuiltin (see Bash Builtins).signalSignal names.
stoppedNames of stopped jobs, if job control is active.
userUser names. May also be specified as
-u.variableNames of all shell variables. May also be specified as
-v.
-C commandcommandis executed in a subshell environment, and its output is used as the possible completions.-F functionThe shell function
functionis executed in the current shell environment. When it is executed, $1 is the name of the command whose arguments are being completed, $2 is the word being completed, and $3 is the word preceding the word being completed, as described above (see Programmable Completion). When it finishes, the possible completions are retrieved from the value of theCOMPREPLYarray variable.-G globpatThe filename expansion pattern
globpatis expanded to generate the possible completions.-P prefixprefixis added at the beginning of each possible completion after all other options have been applied.-S suffixsuffixis appended to each possible completion after all other options have been applied.-W wordlistThe
wordlistis split using the characters in theIFSspecial variable as delimiters, and each resultant word is expanded. The possible completions are the members of the resultant list which match the word being completed.-X filterpatfilterpatis a pattern as used for filename expansion. It is applied to the list of possible completions generated by the preceding options and arguments, and each completion matchingfilterpatis removed from the list. A leading ‘!’ infilterpatnegates the pattern; in this case, any completion not matchingfilterpatis removed.
The return value is true unless an invalid option is supplied, an option other than
-por-ris supplied without anameargument, an attempt is made to remove a completion specification for anamefor which no specification exists, or an error occurs adding a completion specification.compoptcompopt [-o option] [-DEI] [+o option] [name]
Modify completion options for each
nameaccording to theoptions, or for the currently-executing completion if nonames are supplied. If nooptions are given, display the completion options for eachnameor the current completion. The possible values ofoptionare those valid for thecompletebuiltin described above. The-Doption indicates that other supplied options should apply to the “default” command completion; that is, completion attempted on a command for which no completion has previously been defined. The-Eoption indicates that other supplied options should apply to “empty” command completion; that is, completion attempted on a blank line. The-Ioption indicates that other supplied options should apply to completion on the initial non-assignment word on the line, or after a command delimiter such as ‘;’ or ‘|’, which is usually command name completion.If multiple options are supplied, the
-Doption takes precedence over-E, and both take precedence over-IThe return value is true unless an invalid option is supplied, an attempt is made to modify the options for a
namefor which no completion specification exists, or an output error occurs.
Next: A Programmable Completion Example, Previous: Programmable Completion, Up: Command Line Editing [Contents][Index]