How to enable default file completion in bash

前端 未结 2 1165
说谎
说谎 2021-02-05 18:28

Say I have a command named \"foo\" that takes one argument (either \"encrypt\" or \"decrypt\") and then a list of files. I want to write a bash completion script that helps wit

2条回答
  •  花落未央
    2021-02-05 19:09

    I know this is a little late, but I have found a solution here: https://stackoverflow.com/a/19062943/108105

    Basically, you use complete -o bashdefault -o default, and when you want to revert to the default bash completion you set COMPREPLY=(). Here's an example:

    complete -o bashdefault -o default -F _foo foo
    _foo() {
        local cur=${COMP_WORDS[COMP_CWORD]}
        if (( $COMP_CWORD == 1 )); then
            COMPREPLY=( $(compgen -W 'encrypt decrypt' -- "$cur") )
        else
            COMPREPLY=()
        fi
    }
    

提交回复
热议问题