I have been trying to grok how to write zsh completions, but he gap between understanding Create Basic ZSH-Command with Auto-Completion (super simple example and easy to understand) and zsh completions howto got to me and I couldn't figure this out. I am sure there are builtins to do this.
My goal: write a completion function that takes what I have typed, runs a command with it and spits out the result as the completion options. Most likely: take what I typed, grep through the files in some dir for that token and output the result.
This is as far as I have taken it:
runCommandOnFile() {
echo "Run on file: $1"
}
_runCommandOnFile() {
# Does not show grep results like I expect.
# compadd $(ls ~/dir/to/search | grep ${PREFIX})
# Spits out list of all files in dir.
compadd $(ls ~/dir/to/search)
}
compdef _runCommandOnFile runCommandOnFile
I read that ${PREFIX}
holds what was typed, but grepping with this isn't working as I expected.
I see that there are already existing functions to do regex etc, but I cannot see how that relates to an example like mine.
In bash for jump and mark, I achieved this result like so:
_marks_complete() {
local curw=${COMP_WORDS[COMP_CWORD]}
local word=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=()
for file in $(find "${MARKPATH}" -type l -iname "*${word}*" -printf "%f\n") ; do
# If the glob doesn't match, we'll get the glob itself, so make sure
# we have an existing file
COMPREPLY+=( "${file}" )
done
return 0
}
complete -F _marks_complete jump unmark