LinuxCommandLibrary

complete

Customize command-line tab completion

TLDR

Set arguments of a command to autocomplete through a function (completion response is sent in COMPREPLY variable)

$ complete -F [function] [command]
copy

Set arguments of a command to autocomplete through another command ($1 is the command, $2 is the argument the cursor is on, and $3 is the argument preceding the cursor)
$ complete -C [autocomplete_command] [command]
copy

Set arguments of a command to autocomplete to shell builtins
$ complete -b [command]
copy

Apply autocompletion without appending a space to the completed word
$ complete -o nospace -F [function] [command]
copy

List all loaded complete specifications
$ complete -p
copy

List loaded complete specifications for a command
$ complete -p [command]
copy

SYNOPSIS

complete [-abcdefgjksuv] [-pr] [-DE] [-o comp-option] [-A action] [-G globpat] [-P prefix] [-S suffix] [-W wordlist] [-F function] [-C command] [-X filterpat] [-V varname] [name ...]

PARAMETERS

-p
    Display completion specifications for the specified name(s) or all if no name is given.

-r
    Remove completion specifications for the specified name(s) or all if no name is given.

-o comp-option
    Specify a completion option. Common options include default (use default Bash completion), dirnames (complete directory names), filenames (complete filenames), nospace (do not append a space after completion).

-A action
    Specify an action to generate completions. For example, command (command names), file (filenames), variable (variable names).

-G globpat
    Apply globpat (a glob pattern) to the list of possible completions generated by other options.

-P prefix
    Add prefix to each completion generated.

-S suffix
    Add suffix to each completion generated.

-W wordlist
    Specify a static, space-separated list of words from which to complete.

-F function
    Specify a shell function to be executed to generate completions dynamically. This is the most powerful method.

-C command
    Specify an external command to be executed to generate completions. The output of the command is used as the completion list.

-X filterpat
    Apply filterpat to the list of possible completions. Any completion matching the pattern will be removed.

[name ...]
    The name(s) of the command(s) for which completion is being defined, modified, or removed.

DESCRIPTION

The complete builtin command in Bash is used to specify how arguments to a particular command should be completed when the Tab key is pressed. It allows users and shell scripts to define custom programmable completion rules, greatly enhancing productivity by reducing typing and errors. These rules can range from simple word lists to complex functions that dynamically generate possible completions based on context. This mechanism is fundamental to the user-friendly experience of modern command-line interfaces.

CAVEATS

This is a shell builtin command, specifically for GNU Bash. It is not an external executable program found in standard system paths like /usr/bin. Its syntax and behavior may differ significantly in other shells like Zsh (which uses compdef) or Fish. Defining complex completion rules, especially using -F (function) or -C (command), requires good knowledge of shell scripting and the compgen command.

HOW COMPLETION WORKS

When a user types a command and presses the Tab key, Bash looks up if a completion rule has been defined for that command using complete. If a rule exists, Bash executes the specified action (e.g., calls a function, runs an external command, or uses a static wordlist) to generate a list of possible completions. These are then filtered and presented to the user, either directly or after pressing Tab again to cycle through options.

EXAMPLE USAGE

1. Simple word list completion:
To complete arguments for 'mycommand' from a static list:
complete -W "start stop restart status" mycommand

2. Filename completion:
To complete filenames for 'editfile' (default for many commands, but explicit):
complete -f editfile

3. Displaying existing rules:
To display all currently defined completion rules:
complete -p

HISTORY

The concept of programmable command-line completion evolved to greatly enhance the usability of Unix-like shells. Early shells offered basic filename completion. As shell functionality grew, the need for context-aware completion for command arguments became apparent. Bash's complete builtin, introduced in later versions of Bash, provided a powerful and flexible mechanism for users and developers to define highly customized completion behaviors. This marked a significant productivity improvement for shell users, moving beyond simple filename completion to sophisticated argument completion for complex commands and their options.

SEE ALSO

compgen(1), bind(1), enable(1), bash(1), zsh(1)

Copied to clipboard