Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How do I complete from words in a file? (again)



On Jan 11,  4:37pm, Eric Smith wrote:
}
} For example I have keys in the form
} "_what_ever_" in certain files and want to type
} on the command line:
} _wh<tab>

I'm going to assume that "keys in certain files" means one key per line.
If it's more complicated than that, you'll have to describe file format
in more detail.

A week or so ago, you wrote:

} It would be as an argument to particular commands.

So the first general step is to put a file in a directory named in your
$fpath, that begins with a line similar to:

#compdef particular commands ...

where of course "particular commands ..." are actually the names of the
commands for which you want this to complete.  There are other ways to
use #compdef, documented under "Initialization" in the compsys manual.

The rest of the file is the body of the function that looks up possible
matches.  Note that it doesn't have to look up _actual_ matches, only
_possible_ matches -- that is, it can generate extra strings that do
not match and the completion internals will discard those.

In the case you've described, the function body can be very simple:

local keyfile
for keyfile in certain files
do
    compadd ${(f)"$(<$keyfile)"}
done

Again "certain files" there stands in for the actual file names, and the
${(f)"..."} expression is the standard idiom for reading a file and then
splitting it into one quoted shell word per line.

If you need to use a different key file for each command name, you can
either use multiple function files each with a different #compdef line,
or use a case statement instead of the loop above:

local keyfile command=$words[1]
case $command in
 (particular) keyfile=certain;;
 (commands)   keyfile=files;;
esac
compadd ${(f)"$(<$keyfile)"}

If that's not enough to get you where you want to go, you're going to
have to provide a more specific example of what you'd like to have.



Messages sorted by: Reverse Date, Date, Thread, Author