Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to create what you want. See my code below:</p> <pre><code>_tool () { local cur="${COMP_WORDS[COMP_CWORD]}" local first="walk run skip bike" case "${cur}" in *_*_*) local firstsecond=`echo ${cur} | awk -F '_' '{print $1"_"$2}'` local third="${firstsecond}_morning ${firstsecond}_afternoon ${firstsecond}_evening" COMPREPLY=( $(compgen -W "${third}" -- ${cur}) ); return 0;; *_*) local firstcomp=`echo ${cur} | awk -F '_' '{print $1}'` local second="${firstcomp}_home ${firstcomp}_work ${firstcomp}_park" COMPREPLY=( $(compgen -W "${second}" -S "_" -- ${cur}) ); compopt -o nospace; return 0;; *) COMPREPLY=( $(compgen -W "${first}" -S "_" -- ${cur}) ); compopt -o nospace; return 0;; esac } complete -F _tool t </code></pre> <p>I am not much familiar with autocomplete, and it was the first time I used <code>compgen</code> or <code>complete</code>, so I'm not sure I can answer your questions about the theory of the thing. I can tell you what I did, in case it helps you understand how this badly-documented functions work. First I tried to understand what you had. I reproduced your problems successfully. Then I had this idea that, even though you didn't want to list every permutation yourself in the code, I could use the local variables to mimic as if it had. The first thing I did was to get the "first" element after we had the first underscore, and add the second element to it, and feed that to compgen. To my surprise, it worked fine. Then I tried to do the same to the 3rd level... and then problems started. I found out that the second part was not working as fine as I had imagined. after completing the first word, I would get the suggestions for the second; after chosing one word to be the second and hit [TAB], the first underscore would disappear.</p> <pre><code>$ t [tab][tab] # shows options for first part bike_ run_ skip_ walk_ $ t w[tab] # completes first part and appends delimiter $ t walk_[tab][tab] # shows options for second part walk_home_ walk_park_ walk_work_ $ t walk_h[tab] # completes second part and appends delimiter $ t walk_home_[tab][tab] # screws up with it $ t walkhome_ </code></pre> <p>I tried moving the underscore into awk, to no success:</p> <pre><code> local first=`echo ${cur} | awk -F '_' '{print $1"_"}'` local second="${first}home ${first}work ${first}park" </code></pre> <p>For some reason I had to change the variable name to something different from "first", and it started working. Then, on the 3rd element I had the same problem that you described last, where whatever I typed in the 3rd element would disappear, but [tab][tab] would give me the options:</p> <pre><code>$ t walk_home_[tab][tab] # shows options for second part morning afternoon evening $ t walk_home_aft[tab] # erases what I typed $ t walk_home_ </code></pre> <p>To which I added <code>${cur}</code> to compgen in the 3rd word and it worked. This is just a summary of what I did... but I got a working version, so I'm happy with it. :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload