fahd.blog: Writing your own Bash Completion Function
Bash programmable completion is a powerful feature which allows you to specify how arguments to commands should be completed. You do this using thecomplete
command. For example, you can set completion up so that when you type the unzip
command and hit the TAB key, it only shows completions for files ending with the .zip
extension. Similarly, the completion for the ssh
command would display hosts taken from your known_hosts
file. In this post, I will describe how you can write a custom completion function for a command foo
. Bash will execute this function when foo [TAB][TAB]
is typed at the prompt and will display possible completions.
Bash uses the following variables for completion:
COMPREPLY
: an array containing possible completions as a result of your functionCOMP_WORDS
: an array containing individual command arguments typed so farCOMP_CWORD
: the index of the command argument containing the current cursor positionCOMP_LINE
: the current command line
${COMP_WORDS[COMP_CWORD]}
. So, how do you build the result array COMPREPLY
? The easiest way is to use the compgen
command. You can supply a list of words to compgen and a partial word, and it will show you all words that match it. Let's try it out:
Read full article from fahd.blog: Writing your own Bash Completion Function
No comments:
Post a Comment