Bash completion for path in argument (with equals

2019-02-13 11:44发布

问题:

I used to be able to type the following:

$> ./foo --arg=<TAB>

Where foo is any program I wrote, and it would give me a list of files in the current directory, just like tab-completion normally does. I didn't have to make any changes to /etc/bash_completion.

Recently, however, this has gone away for some unknown reason. Does anyone know how to re-enable this feature?

FWIW, this still does the correct thing (notice the lack of an equals sign):

$> ./foo --arg <TAB>

回答1:

I removed all bash completion scripts and started adding them one by one to if any of them cause the problem.

In my case it turned out to be the npm completion script was the cause of this problem.

Not sure (yet) what the problem is, but this is the completion script which caused equal sign values not working as before:

    ###-begin-npm-completion-###
    #
    # npm command completion script
    #
    # Installation: npm completion >> ~/.bashrc  (or ~/.zshrc)
    # Or, maybe: npm completion > /usr/local/etc/bash_completion.d/npm
    #

    COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
    COMP_WORDBREAKS=${COMP_WORDBREAKS/@/}
    export COMP_WORDBREAKS

    if type complete &>/dev/null; then
      _npm_completion () {
        local si="$IFS"
        IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
                               COMP_LINE="$COMP_LINE" \
                               COMP_POINT="$COMP_POINT" \
                               npm completion -- "${COMP_WORDS[@]}" \
                               2>/dev/null)) || return $?
        IFS="$si"
      }
      complete -F _npm_completion npm
    elif type compdef &>/dev/null; then
      _npm_completion() {
        si=$IFS
        compadd -- $(COMP_CWORD=$((CURRENT-1)) \
                     COMP_LINE=$BUFFER \
                     COMP_POINT=0 \
                     npm completion -- "${words[@]}" \
                     2>/dev/null)
        IFS=$si
      }
      compdef _npm_completion npm
    elif type compctl &>/dev/null; then
      _npm_completion () {
        local cword line point words si
        read -Ac words
        read -cn cword
        let cword-=1
        read -l line
        read -ln point
        si="$IFS"
        IFS=$'\n' reply=($(COMP_CWORD="$cword" \
                           COMP_LINE="$line" \
                           COMP_POINT="$point" \
                           npm completion -- "${words[@]}" \
                           2>/dev/null)) || return $?
        IFS="$si"
      }
      compctl -K _npm_completion npm
    fi
    ###-end-npm-completion-###


回答2:

Not sure what environment you're in, but on a recent CentOS

complete -D -o default

enables filename completion after a token w/o whitespace as the default. To toggle it in the other direction:

complete -D -o nospace

However, it looks like older versions of the builtin don't have the -D option.



回答3:

I resolved the same trouble with Ubuntu 12.04 by using https://github.com/ai/rake-completion. You need to

  1. download the file wget -O ~/scripts/rake https://raw.githubusercontent.com/ai/rake-completion/master/rake
  2. add to your .bashrc: . ~/scripts/rake

or You can use one of other ways on that page.