Bash completion for path in argument (with equals sign present)
Asked Answered
A

3

9

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>

Amphigory answered 4/12, 2013 at 1:54 Comment(0)
F
8

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-###
Frants answered 29/7, 2014 at 7:20 Comment(3)
See #10529195Incrocci
Yes they are modifying the COMP_WORDBREAKS variable. Thanks for pointing that out. That clarifies why this script breaks the behaviour of completion overall. I am entering an issue at npm.Requiem
Great job! This was exactly the culprit. Really great job tracking this down.Amphigory
E
1

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.

Exospore answered 4/12, 2013 at 2:38 Comment(5)
My version of Linux (Ubuntu 12.04) does have the -D option, however complete -D -o default did not remedy the problem.Amphigory
FWIW I can toggle the behavior on and off as described in an Ubuntu 12.04 vagrant. Does the behavior still happen in a sanitized bash? (e.g. env -i bash --norc)Exospore
@Amphigory Have you been able to solve it. BTW i can confirm it works as expected in a sanitized bash.Requiem
I was not able to solve the problem although as both you and @bill indicated, using a sanitized bash does work. My guess is there is some bash_completion module that is screwing it up, but I haven't been able to figure out which one.Amphigory
Not exactly the answer to the question, but I was having the same problem and this solved it! Thanks @BillWest!!Smithy
W
1

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.

Whisenhunt answered 21/12, 2014 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.