Skip to content

complete command in MacOS

The complete command in MacOS is used to enable advanced autocompletion features for your shell environment. By setting up completion rules for different commands, options, and arguments, you can enhance your productivity and streamline your workflow. With the complete command, you can customize autocompletion behavior to suit your specific needs and preferences, making your command-line experience more efficient and intuitive.

complete Syntax:

Terminal window
command [option] [parameter]

MacOS complete Options:

OptionDescription
-aDisplay all files
-lUse a long listing format
-RRecursively list subdirectories
-hShow file sizes in a human-readable format

complete Parameters:

ParameterDescription
fileSpecifies the file to be listed

How to use complete command:

Complete file or folder path

Terminal window
complete -f -c cp

Completes file or folder paths when using the “cp” command.

Complete only if a file exists

Terminal window
complete -f -f -X '!*"'"'"(@n.*"'"'",

Completes only if a file exists, useful when combined with specific flags.

Complete command for specific users

Terminal window
complete -u -c ls

Completes the “ls” command only for specific users.

Complete for specific groups

Terminal window
complete -g -c cd

Completes the “cd” command for specific groups of users.

Complete option names

Terminal window
complete -o ls

Completes option names for the “ls” command.

Complete command options

Terminal window
complete -o ls -s '-'

Completes command options with the “ls” command when using a hyphen.

Complete based on pattern matching

Terminal window
complete -g '*(txt|pdf)'

Completes based on pattern matching for files with “txt” or “pdf” extensions.

Complete for specific directories

Terminal window
complete -D /path/to/directory

Completes for specific directories, useful for custom completion of commands related to a specific folder.

How do I use complete in MacOS?

To use the complete command in MacOS, execute the following command:

Terminal window
complete --option <value>

How do I customize tab completion behavior with complete in MacOS?

To customize tab completion behavior using the complete command in MacOS, you can specify different options and values. For example:

Terminal window
complete -W "option1 option2 option3" command

How can I list the completions currently defined in MacOS?

To list the completions currently defined in MacOS, you can use the complete command with the -p option:

Terminal window
complete -p

How do I remove a completion for a specific command using complete in MacOS?

To remove a completion for a specific command in MacOS, you can use the complete command with the -r option followed by the command name:

Terminal window
complete -r command_name

How do I enable autocompletion for a custom command in MacOS?

To enable autocompletion for a custom command in MacOS, you can define a function that generates the desired completion options and then use the complete command to associate that function with your command:

Terminal window
_custom_command_autocomplete() {
# Autocompletion logic
}
complete -F _custom_command_autocomplete custom_command

How do I complete file paths in MacOS using the complete command?

To complete file paths in MacOS using the complete command, you can use the -f option along with the command name:

Terminal window
complete -f command_name

How can I set multiple completion options for a command with complete in MacOS?

To set multiple completion options for a command in MacOS, you can combine different options within a single complete command. For example:

Terminal window
complete -W "option1 option2 option3" -o default command_name

How do I provide completions based on a specific pattern in MacOS using the complete command?

To provide completions based on a specific pattern in MacOS using the complete command, you can utilize the -G option followed by the pattern and the command name:

Terminal window
complete -G 'pattern*' command_name

Applications of the complete MacOS command

  • Execute a command with all possible options and arguments
  • Display help information for a specific command
  • Expand options and arguments from the relevant command’s man page and options
  • Complete file paths or commands when typing in the terminal
  • Improve efficiency and accuracy in the terminal usage by providing autocomplete suggestions.