On Wed, Feb 17, 2021 at 11:28:55AM -0500, Alec Hill wrote:
With dozens of passwords in variously nested directories, it can be hard to remember where one lives. I could spend time better organizing my password folder structure and memorizing this...

I use this path layout for my passwords:

    SITE[/SERVICE]/USERNAME

So:

    example.com/ftp/tomryder

Because I so often have only one file in a directory ancestry like this, it makes sense to me to complete the whole path in one hit if possible. I wrote my own Bash completion to do this:

<https://sanctum.geek.nz/cgit/dotfiles.git/tree/bash/bash_completion.d/pass.bash?h=v10.26.0>

(Attached, too.)

This means I can type:

    $ pass ex<Tab>

And as long as there's only the one password in the structure, it completes to the full path:

    $ pass example.com/ftp/tomryder

This only completes the password names---not e.g. the subcommands---but that's all I wanted to complete anyway.

--
Tom Ryder <https://sanctum.geek.nz/>
Maybe we can bring back the light.
# Load _completion_ignore_case helper function
if ! declare -F _completion_ignore_case >/dev/null ; then
    source "$HOME"/.bash_completion.d/_completion_ignore_case.bash
fi

# Custom completion for pass(1), because I don't like the one included with the
# distribution
_pass() {

    # Iterate through completions produced by subshell
    local ci comp
    while IFS= read -d '' -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

        # Make globs expand appropriately
        shopt -u dotglob
        shopt -s nullglob
        if _completion_ignore_case ; then
            shopt -s nocaseglob
        fi

        # Set password store path
        pass_dir=${PASSWORD_STORE_DIR:-"$HOME"/.password-store}

        # Gather the entries
        for entry in "$pass_dir"/"$2"*.gpg ; do
            entries[ei++]=$entry
        done

        # Try to iterate into subdirs, use depth search with ** if available
        if shopt -s globstar 2>/dev/null ; then
            for entry in "$pass_dir"/"$2"*/**/*.gpg ; do
                entries[ei++]=$entry
            done
        else
            for entry in "$pass_dir"/"$2"*/*.gpg ; do
                entries[ei++]=$entry
            done
        fi

        # Iterate through entries
        for entry in "${entries[@]}" ; do
            # Skip directories
            ! [[ -d $entry ]] || continue
            # Strip leading path
            entry=${entry#"$pass_dir"/}
            # Strip .gpg suffix
            entry=${entry%.gpg}
            # Print shell-quoted entry, null terminated
            printf '%q\0' "$entry"
        done
    )
}
complete -F _pass pass

Reply via email to