user2546460
user2546460

Reputation: 1443

Get autocompletion list in bash variable

I'm working with a big software project with many build targets. When typing make <tab> <tab> it shows over 1000 possible make targets. What I want is a bash script that filters those targets by certain rules. Therefore I would like to have this list of make targets in a bash variable.

make_targets=$(???)
[do something with make_targets]
make $make_targets

It would be best if I wouldn't have to change anything with my project.

How can I get such a List?

Upvotes: 0

Views: 511

Answers (2)

l0b0
l0b0

Reputation: 58908

@yuyichao created a function to get autocomplete output:

comp() {
    COMP_LINE="$*"
    COMP_WORDS=("$@")
    COMP_CWORD=${#COMP_WORDS[@]}
    ((COMP_CWORD--))
    COMP_POINT=${#COMP_LINE}
    COMP_WORDBREAKS='"'"'><=;|&(:"
    # Don't really thing any real autocompletion script will rely on
    # the following 2 vars, but on principle they could ~~~  LOL.
    COMP_TYPE=9
    COMP_KEY=9
    _command_offset 0
    echo ${COMPREPLY[@]}
}

Just run comp make '' to get the results, and you can manipulate that. Example:

$ comp make ''
test foo clean

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158080

You would need to overwrite / modify the completion function for make. On Ubuntu it is located at:

/usr/share/bash-completion/completions/make

(Other distributions may store the file at /etc/bash_completion.d/make)

If you don't want to change the completion behavior for the whole system, you might write a small wrapper script like build-project, which calls make. Then write a completion function for that mapper which is derived from make's one.

Upvotes: 0

Related Questions