help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exclude builtins from command completion


From: kaycee gb
Subject: Re: Exclude builtins from command completion
Date: Fri, 10 Jan 2020 23:31:02 +0000

Le Fri, 10 Jan 2020 09:57:52 -0500,
Chet Ramey <address@hidden> a écrit :

> On 1/9/20 3:17 PM, kaycee gb wrote:
> > Hello,
> > 
> > For one of my projects which works with bash completion, I'm looking for a
> > way to exclude bash builtin commands from the completion proposals. I
> > already had success with exclusion of commands from PATH.
> > 
> > After playing with options for complete -b/-c, compgen -b/-c, these
> > commands are still displayed when I type first 2 letters then <TAB><TAB>
> > 
> > # sh<TAB><TAB>
> > shift shopt show
> > 
> > I want that it matches just the last one.
> > 
> > I even tried with actions from complete enabled/disabled builtins without
> > luck. I'm not sure how actions work.
> > 
> > My bash version is 4.2.053-x86_64-2_slack14.1.
> > 
> > How can I achieve what I want to do ?
> 
> You can't do it on bash-4.2 (which is approximately nine years old) without
> a lot of work.

I know that it starts to be old. But It's just I can't leave my slack 14.1
like this :/
> 
> It's pretty easy to do in bash-5.0. This script will do pretty much what 
> you want:
> 
> buildpat()
> {
>       builtins_pat=
> 
>       set -- $(printf "%q " $(compgen -b -k "$1"))    # to leave
> reserved words, omit -k
>       [ $# -gt 0 ] && builtins_pat='@('"$(IFS='|' ; echo "$*")"')'
> }
> 
> commandcomp()
> {
>       local builtins_pat w
> 
>       w="$2"
>       buildpat "$w"
>       
>       if [ -n "$builtins_pat" ]; then
>               COMPREPLY=( $(shopt -s extglob ; compgen -c -X
> "$builtins_pat" "$w") ) else
>               COMPREPLY=( $(compgen -c "$w") )
>       fi
> }
> 
> complete -I -F commandcomp
> 
> 
Thanks a lot for the full example. 

On another computer I have with bash version 5.0, I red bash man page and I
have seen this -I switch. I was not sure how I should use it. With your help I
tried something basic and yes it does what I want. That's great. 

For the slack pc, I tried to bump bash from slack 14.2 but still too
old, there is no -I :/ 

I made something dirty to have behaviour that approaches what I want.
It's ugly and not perfect but that will do the job for the moment.

Disable nearly all builtins before calling complete

BLTEXCL="compgen|complete|enable|eval|local|set"
BLTINS=$( enable )
BLTINS=${BLTINS//enable /}
for BLTIN in $BLTINS; do  

    if ! [[ "$BLTIN" =~ ^($BLTEXCL)$ ]]; then
      enable -n "$BLTIN"
    fi  
done

I had to enable them again at the beginning of the function called by
complete -D

  BLTINS=$( enable -n )
  BLTINS=${BLTINS//enable -n /}
  for BLTIN in $BLTINS; do
    enable "$BLTIN"
  done

And disable them again at the end of the function. 

I said it's ugly ... 

kaycee,



reply via email to

[Prev in Thread] Current Thread [Next in Thread]