<p>Given a bash function in .bashrc such as</p>
<pre><code>kgp () {
  kubectl get po -n $1 $2
}
</code></pre>
<p>Is it possible to have kubectl auto complete work for k8s resources such as namespaces/pods? As an example if I use</p>
<pre><code>kubectl get po -n nsprefix podprefix
</code></pre>
<p>I can tab auto complete the prefix. Whereas with the positional parameters when I call</p>
<pre><code>kgp nsprefix podprefix
</code></pre>
<p>I have to type out the entire resource name.</p>

<p>Yes, that's because bash-completion only understands <em>known commands</em>, not aliases or new functions that you have made up. You will experience the same thing with a trivial example of <code>alias whee=/bin/ls</code> and then <code>whee &lt;TAB&gt;</code> will do nothing because it doesn't &quot;recurse&quot; into that alias, and <em>for sure</em> does not attempt to call your function in order to find out what arguments it could possibly accept. That could potentially be catastrophic</p>
<p>You're welcome to create a new <a href="https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion" rel="nofollow noreferrer"><code>complete</code></a> handler for your custom <code>kgp</code>, but that's the only way you'll get the desired behavior</p>
<pre class="lang-bash prettyprint-override"><code>_kgp_completer() {
    local cur prev words cword

    COMPREPLY=()
    _get_comp_words_by_ref -n : cur prev words cword
    if [[ $cword == 1 ]] &amp;&amp; [[ -z &quot;$cur&quot; ]]; then
        COMPREPLY=( $(echo ns1 ns2 ns3) )
    elif [[ $cword == 2 ]] &amp;&amp; [[ -z &quot;$cur&quot; ]]; then
        COMPREPLY=( $(echo pod1 pod2 pod3) )
    fi
    echo &quot;DEBUG: cur=$cur prev=$prev words=$words cword=$cword COMPREPLY=${COMPREPLY[@]}&quot; &gt;&amp;2
}
complete -F _kgp_completer kgp
</code></pre>
