Showing only the substrings of COMPREPLY bash completion options to the user
Asked Answered
K

2

6

In a bash completion script, suppose COMPREPLY=(aa/ba/ aa/bb/). When the script is invoked, the completion options looks like this to the user:

$ foo aa/b<TAB>
aa/ba/
aa/bb/

However, I want to have a bit more control over how these options are displayed. In particular, I want to show only a substring of each COMPREPLY option to the user, similar to how directory completion works in bash now:

$ foo aa/b<TAB>
ba/
bb/

Is there any way of doing this in bash?

Koal answered 17/10, 2010 at 1:44 Comment(0)
B
6

I was having the same problem and I fixed it by adjusting how I bound the completion function to the command. I know this works when you are dealing with actual files in the filesystem, I think it will work with any sort of file path like options, but I'm not sure.

Before:

complete -F _fubar fubar

After:

complete -o filenames -F _fubar fubar

For more details: Programmable Completion Builtins

Backslide answered 28/2, 2012 at 17:10 Comment(1)
Awesome! Now I just need to figure out how to selectively turn on/off options for different COMPREPLY cases in my function, without using compoptKoal
R
2

This piece of code taken from debian sid /etc/bash_completion should help:

# Remove colon-word prefix from COMPREPLY items
local colon_word=${1%${1##*:}}
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
    COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
done
Rivero answered 17/10, 2010 at 18:59 Comment(1)
Thanks enzotib. Unfortunately this technique doesn't work, because then the substring replaces the entire completion. That is, foo aa/b<TAB> becomes foo ba/ :-(Koal

© 2022 - 2024 — McMap. All rights reserved.