1 # cdfunc - example completion function for cd
3 # based on the cd completion function from the bash_completion package
5 # Chet Ramey <chet.ramey@case.edu>
7 # Copyright 2011 Chester Ramey
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
14 # TThis program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software Foundation,
21 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 local IFS=$' \t\n' # normalize IFS
26 local cur _skipdot _cdpath
29 # Tilde expansion, with side effect of expanding tilde to full pathname
35 # no cdpath or absolute pathname -- straight directory completion
36 if [[ -z "${CDPATH:-}" ]] || [[ "$cur" == @(./*|../*|/*) ]]; then
37 # compgen prints paths one per line; could also use while loop
39 COMPREPLY=( $(compgen -d -- "$cur") )
41 # CDPATH+directories in the current directory if not in CDPATH
45 # preprocess CDPATH to convert null directory names to .
46 _cdpath=${CDPATH/#:/.:}
47 _cdpath=${_cdpath//::/:.:}
48 _cdpath=${_cdpath/%:/:.}
49 for i in ${_cdpath//:/$'\n'}; do
50 if [[ $i -ef . ]]; then _skipdot=true; fi
52 for j in $( compgen -d -- "$i/$cur" ); do
53 COMPREPLY[k++]=${j#$i/} # cut off directory
56 $_skipdot || COMPREPLY+=( $(compgen -d -- "$cur") )
60 # variable names if appropriate shell option set and no completions
61 if shopt -q cdable_vars && [[ ${#COMPREPLY[@]} -eq 0 ]]; then
62 COMPREPLY=( $(compgen -v -- "$cur") )
65 # append slash to passed directory name that is the only completion.
66 # readline will not do this if we complete from CDPATH
67 if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
68 i=${COMPREPLY[0]} # shorthand
69 if [[ "$cur" == "$i" ]] && [[ "$i" != "*/" ]]; then
76 complete -o filenames -o nospace -o bashdefault -F _comp_cd cd