Prevent path autocompletion from using CDPATH in bash?

The culprit is your bash_completion, not your bash version at all.

With the head version from bash_completion repo, cd auto completion include directories under CDPATH.

You can make change directly to your local bash_completion, but it's better to write your own cd auto completion.


If you want a global effect, create a file named /etc/bash_completion.d/cd with the following content:

_cd()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    _init_completion || return

    local IFS=$'\n' i

    compopt -o filenames

    local -r mark_dirs=$(_rl_enabled mark-directories && echo y)
    local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y)

    _filedir -d

    if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
        i=${COMPREPLY[0]}
        if [[ "$i" == "$cur" && $i != "*/" ]]; then
            COMPREPLY[0]="${i}/"
        fi
    fi

    return 0
}
if shopt -q cdable_vars; then
    complete -v -F _cd -o nospace cd
else
    complete -F _cd -o nospace cd
fi

This version got all from the original, exclude the CDPATH part.


If you want to use only for your setting, you can edit .bashrc with the following:

_my_cd () {
  declare CDPATH=
  _cd "$@" 
}

complete -F _my_cd cd

There's some case made bash completion didn't work with CDPATH in your Mac OSX, your bash_completion is too old or you hadn't load it yet.

You can use BASH_COMPLETION_COMPAT_DIR variable to set the location where bash_completion load your custom completion function, the default is /etc/bash_completion.d.


The correct way would be to use the following in your ~/.bashrc as @Gilles suggested above (shorter version here):

_my_cd () { CDPATH= _cd "$@";}

A better way would be to check CDPATH if and only if no matches are found locally:

_my_cd () { CDPATH= _cd "$@"; if [ -z "$COMPREPLY" ]; then _cd "$@"; fi;}

This way you get the functionality of programmable completion without it being a nuisance. Do not forget

complete -F _my_cd cd

For some reason /usr/share/bash-completion/bash_completion has -o nospace for cd (even though nospace seems to be default), so you might as well have:

complete -F _my_cd -o nospace cd