0

I want to write a small alias aka function to open up vim sessions fast.

vims () {
  vim -S "${HOME}/Sessions/${1}"
}

How can I make bash complete/suggest the session files in the path, when entering vimsTabTab?

uloco
  • 2,283
  • 4
  • 22
  • 37

1 Answers1

1

Based on pynexj answer and my daily use:

_autocomplete()
{
    local cmd=$1 cur=$2 pre=$3 from_path=$4
    local _cur compreply

    _cur=$from_path/$cur
    compreply=( $( compgen -d "$_cur" ) )
    COMPREPLY=( ${compreply[@]#$from_path/} )
    if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
        COMPREPLY[0]=${COMPREPLY[0]}/
    fi
}

vims()
{
    vim -S ~/Sessions/$1
}

_vims_autocomplete()
{
    _autocomplete "$1" "$2" "$3" ~/Sessions
}

complete -F _vims_autocomplete -o nospace vims

# This way allow me to create different alias using the same autocomplete

gogit()
{
    cd ~/git/$1
}

_gogit_autocomplete()
{
    _autocomplete "$1" "$2" "$3" ~/git
}

complete -F _gogit_autocomplete -o nospace gogit

You can get more information about complete here and a tutorial here.

Ozahata
  • 61
  • 3