bash tab completion with spaces

前端 未结 3 1250
失恋的感觉
失恋的感觉 2021-02-06 11:44

I\'m having a problem with bash-completion when the possible options may contain spaces.

Let\'s say I want a function which echoes the first argument:

fu         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 12:21

    If you need to process the data from the string you can use Bash's built-in string replacement operator.

    function _test() {
        local iter use cur
        cur=${COMP_WORDS[COMP_CWORD]}
        use="nick\ mason syd-barrett david_gilmour roger\ waters richard\ wright"
        # swap out escaped spaces temporarily
        use="${use//\\ /___}"
        # split on all spaces
        for iter in $use; do
            # only reply with completions
            if [[ $iter =~ ^$cur ]]; then
                # swap back our escaped spaces
                COMPREPLY+=( "${iter//___/ }" )
            fi
        done
    }
    

提交回复
热议问题