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 the
complete
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 function COMP_WORDS
: an array containing individual command arguments typed so far COMP_CWORD
: the index of the command argument containing the current cursor position COMP_LINE
: the current command line
Therefore, if you want the current argument that you are trying to complete, you would index into the words array using:
${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
However be noted that PDF pages will still not work since PDF viewer itself is sort of an extension, and Chrome doesn't allow extensions running on extensions.