complete / compgen

Programmable completion specification and generation.

Source: src/execution/builtins.f90:3808-4158

Synopsis

complete [-r] [-p] [-W wordlist] [-F function] [-A action] [-o option] [-P prefix] [-S suffix] [-X pattern] command [...]
compgen -W wordlist [prefix]

Description

complete registers, lists, or removes tab-completion rules for commands. compgen generates completions from a word list, useful for testing specs or in completion functions.

complete

Registering Completions

# Static word list
complete -W "start stop restart status" myservice

# Shell function
complete -F _git_complete git

# Built-in action
complete -A directory cd

# Combine options
complete -A file -o filenames -P "--file=" mycommand

Options

FlagArgumentDescription
-WwordlistSpace-separated list of completion words
-FfuncnameShell function to call for completions
-AactionBuilt-in completion action (see below)
-ooptionCompletion behavior option (see below)
-PprefixString prepended to each completion
-SsuffixString appended to each completion
-XpatternFilter pattern — matching completions are removed
-rRemove completion specs for the named commands
-pList all registered specs in reusable format

Actions (-A)

ActionCompletes with
aliasDefined aliases
builtinShell builtins
commandCommands on PATH
directoryDirectories
fileFiles
functionShell functions
hostnameHostnames
keywordShell keywords
variableShell variables
userSystem users
groupSystem groups
serviceSystem services
exportExported variables

Options (-o)

OptionEffect
defaultFall back to default completion if no matches
dirnamesAlso complete directory names
filenamesTreat completions as filenames (add trailing / for dirs)
nospaceDon't add trailing space after completion
plusdirsAdd directory completions to the results
nosortDon't sort completions alphabetically

List and Remove

complete -p           # List all registered specs
complete -r mycommand # Remove spec for mycommand
complete -r           # Remove all specs

compgen

Generate completions from a word list, optionally filtered by a prefix:

compgen -W "start stop restart status" st
# start
# stop
# status

Currently only the -W flag is supported for compgen.

Exit Status

BuiltinStatusCondition
complete0Success
complete1Invalid action, invalid option, or missing arguments
compgen0Matches found
compgen1No matches

Examples

# Complete hostnames for ssh
complete -A hostname ssh

# Custom completion function
_myapp_complete() {
    local cur="${COMP_WORDS[COMP_CWORD]}"
    compgen -W "build test deploy" "$cur"
}
complete -F _myapp_complete myapp

See Also