How to use the command "complete" (with examples)
The “complete” command provides argument autocompletion to shell commands. It allows you to apply a function or a command that performs autocompletion to another command, and also provides the option to apply autocompletion without appending a space to the completed word.
Use case 1: Apply a function that performs autocompletion to a command
Code:
complete -F function command
Motivation: This use case is useful when you want to define a custom function to handle autocompletion for a specific command. By using the “complete” command with the “-F” option, you can specify the function that should be used for autocompletion.
Explanation:
- “complete”: The command to enable autocompletion.
- “-F function”: The “-F” option is used to specify a function that performs autocompletion. Replace “function” with the actual function name.
- “command”: The command for which autocompletion should be applied.
Example output: If we have a custom function called “custom_complete” that performs autocompletion for the “mycommand” command, we would use the following command:
complete -F custom_complete mycommand
Use case 2: Apply a command that performs autocompletion to another command
Code:
complete -C autocomplete_command command
Motivation: This use case is useful when you want to use an external command to perform autocompletion for a specific command. By using the “complete” command with the “-C” option, you can specify the command that should be used for autocompletion.
Explanation:
- “complete”: The command to enable autocompletion.
- “-C autocomplete_command”: The “-C” option is used to specify a command that performs autocompletion. Replace “autocomplete_command” with the actual command that performs autocompletion.
- “command”: The command for which autocompletion should be applied.
Example output: If we have an external command called “autocompletion_script” that performs autocompletion for the “mycommand” command, we would use the following command:
complete -C autocompletion_script mycommand
Use case 3: Apply autocompletion without appending a space to the completed word
Code:
complete -o nospace -F function command
Motivation: This use case is useful when you want to avoid appending a space to the completed word during autocompletion. It can be helpful in cases where the completed word should be immediately followed by another parameter or argument, without a space in between.
Explanation:
- “complete”: The command to enable autocompletion.
- “-o nospace”: The “-o” option is used to specify an option for autocompletion. In this case, “nospace” indicates that a space should not be appended to the completed word.
- “-F function”: The “-F” option is used to specify a function that performs autocompletion. Replace “function” with the actual function name.
- “command”: The command for which autocompletion should be applied.
Example output: If we have a custom function called “custom_complete” that performs autocompletion for the “mycommand” command, and we want to avoid appending a space to the completed word, we would use the following command:
complete -o nospace -F custom_complete mycommand
Conclusion:
The “complete” command provides powerful autocompletion capabilities for shell commands. It can be used to apply custom functions or external commands to handle autocompletion, and also allows you to control whether a space should be appended to the completed word. By understanding and using the various options and arguments of the “complete” command, you can enhance the usability and efficiency of your shell scripts and command line interactions.