Zypper in package name autocompletion in WSL

It has nothing to do with _zypper completion function from zypper. What is output of “type _zypper”?

~> type _zypper
-bash: type: _zypper: not found 

On “normal” Leap this function is defined in /etc/bash_completion.d/zypper.sh. Does it exist? Does it contain definition of _zypper function?

Yes and

~> cat /etc/bash_completion.d/zypper.sh

_zypper() {
        ZYPPER="$(type -P zypper)"


        local noglob=$(shopt -po noglob)
        local comp cur prev command
        local -a opts=()
        local -i ITER=0
        local IFS=$'
'


        # Do not expand `?' for help
        set -o noglob


        if test ${#ZYPPER_CMDLIST[@]} -eq 0; then
                ZYPPER_CMDLIST=($(
                        {
                                zypper -q subcommand;
                                LC_ALL=POSIX zypper -q -h | sed -n '/[Cc]ommands:$/,$ s/^ \{6\}\([^ ,]\+\(,[ *][^ ,]\+\)*\).*/\1/p';
                        } | sed 's/, */
/g'
                ))
        fi


        if test $COMP_CWORD -lt 1 ; then
                let COMP_CWORD=${#COMP_WORDS[@]}
        fi
        prev=${COMP_WORDS[COMP_CWORD-1]}
        cur=${COMP_WORDS[COMP_CWORD]}


        let ITER=COMP_CWORD
        while test $((ITER--)) -ge 0 ; do
                comp="${COMP_WORDS[ITER]}"
                # bsc#1049826 - surrounding ' 's are important to get a word-match of ${comp}:
                if [[ " ${ZYPPER_CMDLIST[@]} " =~ " ${comp} " ]]; then
                        command=${COMP_WORDS[ITER]}
                        break;
                fi
                if [[ "${comp}" =~ "zypper" ]]; then
                        command="zypper"
                        break;
                fi
        done
        unset ITER comp


        case "$prev" in
                "--type" | "-t")
                        opts=(package patch pattern product srcpackage)
                        COMPREPLY=($(compgen -W "${opts
[li]}" -- ${cur}))[/li]                        _strip
                        eval $noglob
                        return 0;
                ;;
                "--repo" | "-r" | "--from")
                        opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q lr | \
                                sed -rn '/^[0-9]/{
                                        s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/
                                        s/[[:blank:]]*$//
                                        p
                                }'))
                        COMPREPLY=($(compgen -W "${opts
[li]}" -- ${cur}))[/li]                        _strip
                        eval $noglob
                        return 0;
                ;;
        esac
        unset prev


        if [[ "$command" =~ "zypper" ]]; then
                opts=(${ZYPPER_CMDLIST
[li]}$(echo; LC_ALL=POSIX $ZYPPER -q help 2>&1 | \[/li]                        sed -n '/[Oo]ptions:$/,/[Cc]ommands:$/ s/^[[:blank:]]*\(--[^[:blank:],[]*\).*/\1/p'))
                COMPREPLY=($(compgen -W "${opts
[li]}" -- ${cur}))[/li]                _strip
                eval $noglob
                return 0;
        fi


        if test -n "$command" ; then
                if ! [[ $cur =~ ^[^-] ]] ; then
                        opts=$(LC_ALL=POSIX $ZYPPER -q help $command 2>&1 | sed -n '1,/[Oo]ptions:$/d; s/^.\{,6\}\(--[^[:blank:][]*\).*/\1/p')
                fi


                #handling individual commands if they need more then we can dig from help
                if ! [[ $cur =~ ^- ]] ; then
                case "$command" in
                        help | \?)
                                opts=(${ZYPPER_CMDLIST[@]})
                        ;;
                        removerepo | rr | modifyrepo | mr | renamerepo | nr | refresh | ref)
                                opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q lr | \
                                        sed -rn '/^[0-9]/{
                                                s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/
                                                s/[[:blank:]]*$//
                                                /^$/d
                                                p
                                        }'))
                        ;;
                        addservice | as | modifyservice | ms | removeservice | rs)
                                opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q ls | \
                                        sed -rn '/^[0-9]/{
                                                s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/
                                                s/[[:blank:]]*$//
                                                /^$/d
                                                p
                                        }'))
                        ;;
                        removelock | rl)
                                opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q ll | \
                                        sed -rn '/^[0-9]/{
                                                s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/p
                                                s/[[:blank:]]*$//
                                                /^$/d
                                                p
                                        }'))
                        ;;
                        product-info)
                                opts=(${opts[@]}$(echo; _available_solvables product ))
                        ;;
                        pattern-info)
                                opts=(${opts[@]}$(echo; _available_solvables pattern ))
                        ;;
                        patch-info )
                                opts=(${opts[@]}$(echo; _available_solvables patch ))
                        ;;
                        remove | rm | update | up)
                                opts=(${opts[@]}$(echo; _installed_packages ))
                        ;;
                        install | in | source-install | si | download | info | if | addlock | al)
                                opts=(${opts[@]}$(echo; _available_packages ))
                        ;;
                esac
                fi
                IFS=$'
'
                COMPREPLY=($(compgen -W "${opts
[li]}" -- ${cur}))[/li]                _strip
        fi
        eval $noglob
}

If it is really the content of this file it is missing several more functions and it is missing completion definition itself. Looking at WSL package on openSUSE download pages, file is the same as on “normal” Leap.

Content of /etc/bash_completion.d is sourced in /etc/bash.bashrc and this file seems to exist in WSL archive as well. Sorry, you have to debug what happens on your system. I have no idea how WSL is started or how bash is invoked there and what startup files are really used.

I was giving you just the short version that shows the definition of _zypper. Honestly, I don’t know why WSL is like this either. For Leap 42.3 WSL, no problem but now with LEAP 15.1 and 15.2, some things operate quite funky…

Although involves a little bit of guessing, I cannot imagine how zypper would work any differently in WSL than in a “normal” Linux environment.

First a question that may not be important…
Is your WSL v1 or v2?
The essential requirement to set up WSL2 is to enable Hyper-V in the Windows Advanced Program settings. If you turned on only WSL, then you are running WSL v1. I won’t go into the differences which can be significant setting up things like networking and the environment, but Id like to get this basic question answered now regardless if it turns out to be important to your problem or not.

The next thing I’d like you to do is stop using “sudo.” When you invoke it, it’s effective only for that command. openSUSE is different than other distros in that we support long running elevated permissions with either of the following

# Following elevates to root permissions but retains aspects of the normal User environment like paths. Useful for instance if you are already in your working directory and don't want to be located to another directory

su

# If you require a full root environment as though you logged in as root and you need to support a path to root's user directory, then the following will re-log you in as root

su -


# For both of the above, when you have completed your work with root permissions and wish to return to your normal User permissions, repeat the command you used and add your Normal User name afterwards. You will notice your cursor will return to a right angle instead of hash. The following example assumes you originally ran the command that re-logs you in as root and now you want to return to a User account named SJLPHI

su - SJLPHI


OK,
Now I suspect that what’s happening is that your package cache hasn’t been populated, you simply installed the openSUSE LEAP image into WSL and that was it. First , update your leap which will do two things at once. You will automatically invoke a refresh which will populate your package cache and you’ll also download and install all the packages necessary to bring your openSUSE up to date. Of course, you’ll have to run the following command with elevated permissions

zypper up

Now, I’m guessing that your name completion for package names should be working. Install your software tabbing to complete if you wish.

HTH,
TSU

In the way you’ve described, I’m pretty sure I am using WSLv1. Also, one of the first things that I do after installation is refreshing and upgrading LEAP. I think the cache “should” be populated.

This is just a very weird phenomena because I don’t have trouble with the older versions that were available on MS store (LEAP 42.2 for example). I can confirm the missing/broken for the LEAP 15.2 WSL.

Actually nevermind, I just checked again in my other machines, autocompletion doesn’t work on WSL LEAP 42, 15.1 or 15.2. I read some form of incompatibility in filesystems. It’s not only for WSL Leap 15.1/15.2