Bash completion ignores for "cd" command

Quintin Beukes quintin at skywalk.co.za
Sat Oct 17 19:14:31 BST 2009


Hey,

Sometimes when you "cd" and [tab] for the auto complete, there are
directories which you don't want to include. Some examples include
maybe not wanting hidden directories, or my personal preference, not
wanting to include .svn directories. When you browse a working copy
there is no point to see the .svn directories in the completions. If
you really want to navigate to it, then disable this behavior or
explicitly type .svn.

Either way, I implemented this. I'm not sure if this is the correct
place to report it, though I figured completions are mostly distro
specific, so I'll report it here first.

Also, because of the many versions of these files it was difficult to
create a patch, so I'll summarize the changes here.

In bash_completion.d, there is an extra file called "cd-ignores". It contains:
----SNIP: /etc/bash_completion.d/cd-ignores----
# Some ignores for the cd command

# Add CD_IGNORE_HIDDEN=1 to your ~/.bashrc file to have cd ignore
hidden directories
if [ -n "$CD_IGNORE_HIDDEN" ]; then
 cd_addexclude ".*|*/.*"
fi
----SNIP-END: /etc/bash_completion.d/cd-ignores----

And the "subversion" file contains the following in the beginning:
----SNIP: /etc/bash_completion.d/subversion----
....header comments....
# setting CD_INCLUDE_DOTSVN in your ~/.bashrc will cause .svn directories to
# be included in the completions of the "cd" builtin command
if [ -z "$CD_INCLUDE_DOTSVN" ]; then
       cd_addexclude '*.svn'
fi
....the rest of the code....
----SNIP-END: /etc/bash_completion.d/subversion----

And finally, in the bash_completion script, there are 4 lines which
configure the "cd" command completions, they are:
if shopt -q cdable_vars; then
   complete -v -F _cd $nospace $filenames cd
else
   complete -F _cd $nospace $filenames cd
fi

They are replaced with:
----SNIP: /etc/bash_completion----
# adds a path match to exclude from "cd" builtin
# Usage examples:
#   cd_addexclude '*.svn'
#   cd_addexclude 'build|target'
_CD_EXCLUDEARG="" # empty for when someone "reimports" /etc/bash_completion
cd_addexclude()
{
       if [ -z "$1" ]; then
         return
       fi

       if ! [ -z "$_CD_EXCLUDEARG" ]; then
               _CD_EXCLUDEARG="$_CD_EXCLUDEARG|$1"
       else
               _CD_EXCLUDEARG="$1"
       fi

       _cd_configure
}
function _cd_configure()
{
       local tmp=""
       if [ -n "$_CD_EXCLUDEARG" ]; then
           tmp=(-X "@($_CD_EXCLUDEARG)")
       fi

       if shopt -q cdable_vars; then
           complete -v -F _cd "${tmp[@]}" $nospace $filenames cd
       else
           complete -F _cd "${tmp[@]}" $nospace $filenames cd
       fi
}
_cd_configure
----SNIP-END: /etc/bash_completion----

So this one defines 2 excludes, for hidden directories and .svn
directories. The .svn directories can be included by defining
"CD_INCLUDE_DOTSVN" in your ~/.bashrc, or the hidden directories can
be excluded from the complete by defining CD_IGNORE_HIDDEN in your
~/.bashrc script. The hidden exclude isn't optimal, because you might
not want it to be included by default, but you would want it if you
start typing a hidden filename and then want it to complete it, like
"cd somedir/" won't show hidden directories, but "cd somedir/." or "cd
somedir/.m" will show only hidden directories or only hidden
directories starting with ".m" respectively.

Quintin Beukes



More information about the ubuntu-devel mailing list