compopt
Modify or display completion options
TLDR
Print the completion options for given command
Enable or disable a completion option of a command
Print the options for the currently executing completion
Enable or disable a completion option of a command
SYNOPSIS
compopt [-o option] [+o option] [-D] [name ...]
PARAMETERS
-o option
Adds the specified completion option for the given command(s). Common options include 'nospace', 'nosort', 'plusdirs', 'filenames', 'dirnames', 'default', 'bashdefault', 'noquote', 'unfiltered', and 'extdebug'.
+o option
Removes the specified completion option for the given command(s).
-D
Clears all previously set completion options for the specified command(s).
name ...
One or more command names for which completion options are to be modified. If no name is provided, options for the command currently being completed are modified.
DESCRIPTION
compopt is a bash built-in command used to fine-tune the behavior of programmable completion for specific commands. It allows you to add, remove, or clear completion options previously set using the `complete` command, or to modify options for the command currently being completed. This command is crucial for advanced customization of shell completion, enabling precise control over how Bash suggests completions for command arguments, options, and filenames.
For instance, one can specify that a command's arguments should not have a trailing space added after completion (`nospace`), or that directory names should always be included in the suggestions (`plusdirs`). Unlike `complete -o`, which sets options for an entire completion specification, `compopt` provides the flexibility to adjust these options dynamically, often within a completion function itself, based on the context of the completion. This makes `compopt` an invaluable tool for developers writing sophisticated completion scripts, ensuring a smoother and more intuitive command-line experience for users. It is an integral part of Bash's powerful completion system.
CAVEATS
compopt is a bash built-in command; it is not a standalone executable found in `$PATH`. This means its availability and behavior are tied directly to the Bash shell. It is primarily used within programmable completion functions to dynamically adjust completion behavior based on the current context. Its effects are local to the current shell session unless the completion settings are loaded from a shell startup file (e.g., `.bashrc`, `.bash_completion`).
Using `compopt` outside of a completion function or without an active completion attempt may not yield the expected results, as it often operates on the state of the current completion word.
INTERACTION WITH 'COMPLETE -O'
While `complete -o option` sets global options for a completion specification, `compopt -o option` can be used within a completion function to override or modify these options dynamically for the current completion attempt. For example, a completion function might initially suggest filenames but then use `compopt -o nospace` if a particular argument is recognized as the last argument in a command. This dynamic control is a key strength of `compopt`.
DEFAULT BEHAVIOR
When no name is provided to `compopt`, it modifies the options for the current command being completed. This is its most common usage within completion functions, allowing the function to influence the completion behavior specific to the exact context of the completion call.
HISTORY
The `compopt` command was introduced as part of the Bash Programmable Completion system. This system significantly enhanced Bash's ability to provide sophisticated command-line completion beyond simple filename or command name expansion. Programmable completion, including commands like `complete`, `compgen`, and `compopt`, was a major feature addition that aimed to provide powerful, customizable completion capabilities, similar to those found in other shells like `zsh`. Its development was driven by the need for more intelligent and context-aware completion scripts, allowing users to complete not just commands and files, but also arguments, options, and even values specific to certain commands. It has been a standard part of Bash for many years, evolving with the shell itself.