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] [-o comp] [-DEI] [-x expr] [-F func] [-G glob] [-P prefix] [-S suffix] [-W wordlist] [-C command] name [name ...]

PARAMETERS

-A action
    use specified action to generate matches (e.g., aliases, directories, files)

-a
    alias names

-b
    key binding names (for bind)

-c
    external command names

-C command
    use output of command as completion matches

-d
    directory names

-D
    define default completion for unspecified commands

-E
    environment variable names

-e
    exported environment variable names

-f
    ordinary filenames

-F function
    use named function for completions

-G globpat
    match glob pattern

-g
    group names

-I
    names of integer variables

-j
    job names

-k
    shell reserved words (keywords)

-l
    shell reserved words after 'for' or 'case'

-m
    named directories (in hash table)

-o comp-type
    behavior modifier (e.g., bashdefault, default, dirnames, filenames, nospace, plusdirs)

-P prefix
    add prefix to each completion display

-r
    remove completion for command(s)

-s
    non-builtin command names

-S suffix
    add suffix to each completion display

-t
    typeset variable names

-u
    user names

-v
    variable names

-W wordlist
    use comma-separated wordlist

-x expr
    use expr as filter; display matches only if expr non-null

DESCRIPTION

The complete command is a Bash shell builtin used to specify filename completion and/or command completion for specific commands. It enables customizable tab-completion behaviors, making the shell suggest context-aware options, arguments, files, or other data when the Tab key is pressed.

This is essential for advanced users and scripts, allowing completions based on functions, globs, word lists, external commands, or predefined actions (e.g., hostnames, jobs). For instance, complete -F _git git uses a function for Git-specific suggestions like branches or remotes.

Completions can filter results, add prefixes/suffixes, or integrate with Bash's readline library. Predefined completions (e.g., for apt, ssh) often rely on the bash-completion framework, loaded via /etc/bash_completion. Use complete -p to inspect settings and complete -r to remove them.

It's key for productivity, reducing typing errors and speeding workflows in interactive shells.

CAVEATS

Bash-specific builtin; unavailable in sh, zsh, etc. Completion functions must be loaded (e.g., via bash-completion package). Overly complex setups may slow shell startup. Use complete -p | grep name to debug.

COMMON ACTIONS

-A supports: aliases, arrayvar, bindable, bindnames, builtin, command, directory, dirname, disabled, enabled, export, file, filedir, filename, function, globpat, group, groups, help, hostname, hostnames, index, job, jobs, keyword, namedir, option, options, owner, ownedir, pager, pattern, plugin, reserved, running, service, setopt, signal, stopped, trackdir, uid, uiddir, var, variable, word.

EXAMPLE USAGE

complete -W 'foo bar baz' mycmd for static words.
complete -F _longopt myprog for function-based (define _longopt).
complete -r sudo to reset.

HISTORY

Present since Bash 1.14 (1994); expanded in Bash 2.x+ with -o, -F options for readline integration. Bash 4.0+ added plugin support (-O). Integral to bash-completion project since 2000s.

SEE ALSO

compgen(1), compopt(1), bind(1), bash(1)

Copied to clipboard