[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Behavior of 'complete -o filenames' unclear
From: |
Michael Siegel |
Subject: |
Re: [Help-bash] Behavior of 'complete -o filenames' unclear |
Date: |
Sun, 14 Oct 2018 20:02:20 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux i686; rv:60.0) Gecko/20100101 Thunderbird/60.0 |
Am 10.10.18 um 17:36 schrieb Chet Ramey:
>
> If you want to prevent readline from quoting filenames that appear in
> the current directory by removing `-o filenames', you can use one of
> the shell facilities that quotes arguments to add the quotes back: printf's
> `%q' format or the new-in-bash-4.4 @Q parameter expansion
>
>> Apropos putting file names into COMPREPLY: After some further discussion
>> in #bash on Freenode, I have now sanitized the way this is done:
>>
>> while IFS=$'\n' read -r line
>> do
>> COMPREPLY+=("$line")
>> done < <(compgen -f -- "$dbm_dir/${COMP_WORDS[COMP_CWORD]}" | \
>> cut -d '/' -f 5)
>
> You could avoid the pipe by using something like
> "address@hidden/}" to remove the leading directory name.
Following both of your suggestions, I've now created a version that
should be good to go. I've had it tested by shellcheck, which didn't
find any reason to complain.
Here's the script:
_dbm_complete_bash() {
local dbm_dir="$HOME/.config/dbm"
# Only perform bookmark name completion on the first argument to dbm,
# except when that first argument is -d (the delete switch). In that
# case, perform bookmark name completion on any given argument.
if [ "address@hidden" -lt 3 ] || [ "${COMP_WORDS[1]}" = '-d' ]
then
# Put the names of all files found in $dbm_dir into the COMPREPLY
# array and omit their directory prefix.
# Use IFS=$'\n' for read, as compgen cannot generate null-delimited
# output.
# Use printf '%q' to have shell special characters escaped properly.
while IFS=$'\n' read -r line
do
COMPREPLY+=("$(printf '%q' "${line#"$dbm_dir/"}")")
done < <(compgen -f -- "$dbm_dir/${COMP_WORDS[COMP_CWORD]}")
fi
}
complete -F _dbm_complete_bash dbm