completion 

Send to Kindle
home » snippets » bash » completion



optcomplete.py version

# Autocompletion support - bash function for the optcomplete module
# http://furius.ca/optcomplete/

_ck_optcomplete()
{
  COMPREPLY=( \
    $( COMP_LINE="$COMP_LINE" COMP_POINT="$COMP_POINT" \
       COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD="$COMP_CWORD" OPTPARSE_AUTO_COMPLETE=1 \
       "$1" ) \
    )
}
complete -F _ck_optcomplete foo.py

Examples

Screen Sessions

# Invoke auto completion for a command.
# COMPREPLY - should contain the array of possible completions
# COMP_WORDS - all the words on the command line
# COMP_CWORD - index into COMP_WORDS for the current word that is being completed

_complete_screensessions() 
{
    local currentWord
    currentWord="${COMP_WORDS[COMP_CWORD]}"
    screenSessions="$(screen -ls | awk -F "\t" '{ n=split($2,arr,".") ; if (n==2) print arr[2] }')"
    COMPREPLY=( $(compgen -W "$screenSessions" -- "$currentWord") )
    return 0
}
complete -o bashdefault -F _complete_screensessions scr_custom

My vim Sessions

complete -o bashdefault -W '$(cd $VIMWS && ls *.vs | sed s/.vs$//)' vs