When bash-completion is loaded, unrar x completes after pressing tab to RAR-archives in the directory.
But for multipart archives with the new naming convention, like
Filename.part01.rar
Filename.part02.rar
Filename.part03.rar
it doesn't see any difference between the first archive ending on .part1.rar, .part01.rar or .part001.rar and all the others parts like .part02.rar which are never opened directly, it completes them all.
Is it possible to configure bash-completion so that only the first part of multipart RAR-archives is completed? This means files which end on .rar but must not end on .part□.rar where □ is a number greater than 1 with leading zeros (e.g. 2 or 02 or 002)?
The following works for me. I DO NOT know if this is 100% correct:
# unrar(1) completion -*- shell-script -*-
_unrar()
{
local cur prev words cword cmp_opts=1 i
_init_completion || return
# Check if all of the middle part are options.
# If not, we break at the last-option idx, and won't complete opts.
for ((i=1; i<${#words[@]}-1; i++)); do
# not using the whole list for checking -- too verbose
if [[ ${words[i]} != -* || ${words[i]} == '--' ]]; then
cmp_opts=0
break
fi
done
if [[ $cur == -* ]] && ((cmp_opts)); then # options
COMPREPLY=( $( compgen -W '-ad -ap -av- -c- -cfg- -cl -cu -dh -ep -f
-idp -ierr -inul -kb -o+ -o- -ow -p -p- -r -ta -tb -tn -to -u -v
-ver -vp -x -x@ -y' -- "$cur" ) )
elif ((cword == 1)); then # command
COMPREPLY=( $( compgen -W 'e l lb lt p t v vb vt x' -- "$cur" ) )
elif ((cword == i+1)); then # archive
_filedir '[rR][aA][rR]'
# If there is a second, third, ... ninth part
for i in "${COMPREPLY[@]}"; do
if [[ $i == *.part*(0)[2-9].[rR][aA][rR] ]]; then
# Only look for the first, since it's the only useful one
COMPREPLY=()
_filedir 'part*(0)1.[rR][aA][rR]'
break
fi
done
else # files.../path...
_filedir
fi
} &&
complete -F _unrar unrar
# ex: ts=4 sw=4 et filetype=sh
/etc/bash_completion.d/with one file per command. I am not fully familiar with the synthax, but maybe you'll have an idea by looking at it?