0

I want to make a "completion script" that substitutes a virtual command (g) to another command (cd).

I want g dl<Tab> to dynamically transform into cd ~/dl/.

An alias is not what I want, because I want to continue typing. The g's argument is retrieved using a key/value store

I tried to assign values to the $COMP_WORDS array to change user written text, but it doesn't works :

_g_completions() {
    local FILE="$HOME/.config/kv.awk/bookmarks.txt"
    local COMPLEN="${#COMP_WORDS[@]}"
    if [ "${COMP_WORDS[$COMP_CWORD]}" -lt 3 ]
    then
        # list keys
        COMPREPLY=($(compgen -W "$(awk '/^[A-Za-z]/ {print tolower($1)}' "$FILE")" -- "$CURRENT"))
    else
        # subsitute command
        COMP_WORDS[0]="cd"
        COMP_WORDS[1]="$(kv.awk bookmarks get "${COMP_WORDS[1]}")"
    fi
}

complete -F _g_completions g
ppom
  • 1
  • 3

0 Answers0