I was working with the shell, and by mistake I autocompleted with tab after writing _e, which resulted in _expand.

What does this command do? I couldn't find an explanation online, the only references I could find here on Ask Ubuntu were:

  • Cannot change zsh prompt
  • Terminal autocomplete functionality can only match from the beginning?

But they don't answer my question. Instead, they open up more questions of the same kind about commands like _complete, _complete_as_root, etc.


Solution 1:

You can find out what _expand does, when typing

$ type _expand
_expand is a function
_expand ()
{
    if [[ "$cur" == \~*/* ]]; then
        eval cur=$cur;
    else
        if [[ "$cur" == \~* ]]; then
            cur=${cur#\~};
            COMPREPLY=($( compgen -P '~' -u "$cur" ));
            [ ${#COMPREPLY[@]} -eq 1 ] && eval COMPREPLY[0]=${COMPREPLY[0]};
            return ${#COMPREPLY[@]};
        fi;
    fi
}

This is a function in the bash completion mechanism. It expands tildes (~) in pathnames. In /etc/bash_completion is a comment about the function:

# Expand ~username type directory specifications.  We want to expand
# ~foo/... to /home/foo/... to avoid problems when $cur starting with
# a tilde is fed to commands and ending up quoted instead of expanded.

Try it in a terminal, type:

~<tab><tab>

It will expand to the usernames, for example

~usera     ~userb     ~userc