diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-05 14:19:53 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-05 14:19:53 +1200 |
commit | bad3e29c3ff0304821414219c455e1edfd88615c (patch) | |
tree | b8e40ba588f4faa168ee09e35307c128663b5e28 /bash | |
parent | Complete kill builtin with jobspecs and user PIDs (diff) | |
download | dotfiles-bad3e29c3ff0304821414219c455e1edfd88615c.tar.gz dotfiles-bad3e29c3ff0304821414219c455e1edfd88615c.zip |
Add find(1) completion
Diffstat (limited to 'bash')
-rw-r--r-- | bash/bash_completion.d/find.bash | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/bash/bash_completion.d/find.bash b/bash/bash_completion.d/find.bash new file mode 100644 index 00000000..94305a8e --- /dev/null +++ b/bash/bash_completion.d/find.bash @@ -0,0 +1,85 @@ +# Semi-intelligent completion for find(1); nothing too crazy +_find() { + + # Backtrack through words so far; if none of them look like options, we're + # still completing directory names + local -i opts + for ((i = COMP_CWORD; i >= 0; i--)) ; do + case ${COMP_WORDS[i]} in + -*) + opts=1 + break + ;; + esac + done + if ! ((opts)) ; then + compopt -o dirnames + return + fi + + # For the rest of this, if we end up with an empty COMPREPLY, we should + # just do what Bash would normally do + compopt -o bashdefault -o default + + # Iterate through whatever the subshell gives us; don't add blank items, though + while read -r item ; do + [[ -n $item ]] || continue + COMPREPLY[${#COMPREPLY[@]}]=$item + done < <( + + # If the word being completed starts with a dash, just complete it as + # an option; crude, but simple, and will be right the vast majority of + # the time + case ${COMP_WORDS[COMP_CWORD]} in + -*) + compgen -W ' + -atime + -ctime + -depth + -exec + -group + -links + -mtime + -name + -newer + -nogroup + -nouser + -ok + -perm + -print + -prune + -size + -type + -user + -xdev + ' -- "${COMP_WORDS[COMP_CWORD]}" + ;; + esac + + # Otherwise, look at the word *before* this one to figure out what to + # complete + case "${COMP_WORDS[COMP_CWORD-1]}" in + + # Args to -exec and -execdir should be commands + -exec|-execdir) + compgen -A command -- "${COMP_WORDS[COMP_CWORD]}" + ;; + + # Args to -group should complete group names + -group) + compgen -A group -- "${COMP_WORDS[COMP_CWORD]}" + ;; + + # Legal POSIX flags for -type + -type) + compgen -W 'b c d f l p s' -- "${COMP_WORDS[COMP_CWORD]}" + ;; + + # Args to -user should complete usernames + -user) + compgen -A user -- "${COMP_WORDS[COMP_CWORD]}" + ;; + esac + ) +} +complete -F _find find |