Using command output to generate autocomplete commands for zsh

Hey, I'm trying to get zsh to run the git command and use the output to generate autocomplete capabilities.

The command I'm trying to run is

git log -n 2 --pretty=format:"'%h %an'"

And here is the code I'm using:

local lines words

lines=(${(f)$(git log -n 2 --pretty=format:"'%h %an'")})
words=${(f)$(_call_program foobar git log -n 2 --pretty=format:"%h")}

echo "Length of lines is " ${#lines[@]} " value is " ${lines}
echo "Length of words is " ${#words[@]} " value is " ${words}

compadd -d lines -a -- words

This does not work at all ... he thinks that wordsthis is the only element and the lines do not get the proper print.

However, when I try to adjust the string array manually, it all works.

local lines words

lines=('one two' 'three')
words=('one two' 'three')

echo "Length of lines is " ${#lines[@]} " value is " ${lines}
echo "Length of words is " ${#words[@]} " value is " ${words}

compadd -d lines -a -- words
+3
source share
3 answers

To make words be an array, you should use

words=( ${(f)...} )

or

set -A words ${(f)...}

. words=${(f)...}, . , ${(f)...}, lines, words?

, : ${(f)$(...)} ${(f)"$(...)"}. - : , , - , - , stackoverflow.

+3

, ZyX, script ,

local lines words

lines=(${(f)"$(git log -n 15 --pretty=format:"'%h - %an - %s'")"} )
words=(${(f)"$(git log -n 15 --pretty=format:"%h")"})

compadd -l -d lines -a -- words
0

I had a more difficult situation. I tried to grep a lot of files for a line and then edit the resulting file list. Using wildcards ** and * did not allow the above solution to work for me. I did this for work, breaking down into 2 steps:

> tmp=$(grep -l someString **/*.clj)
> fileList=( ${(f)tmp} )
0
source

All Articles