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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.