From e2307ee1116f4d1164587bd78f58d143e4bbb397 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 5 Jun 2015 23:08:06 +1200 Subject: Thorough completion for path --- bash/bashrc.d/path.bash | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/bash/bashrc.d/path.bash b/bash/bashrc.d/path.bash index 9ecf1e9a..4e417d32 100644 --- a/bash/bashrc.d/path.bash +++ b/bash/bashrc.d/path.bash @@ -166,5 +166,49 @@ path() { ;; esac } -complete -W 'help list insert append remove set check' path + +# Completion for path +_path() { + local word=${COMP_WORDS[COMP_CWORD]} + + # Complete operation as first word + if ((COMP_CWORD == 1)) ; then + COMPREPLY=( $(compgen -W \ + 'help list insert append remove set check' \ + -- "$word") ) + else + case ${COMP_WORDS[1]} in + + # Complete with one directory + insert|i|append|a|check|c) + if ((COMP_CWORD == 2)) ; then + COMPREPLY=( $(compgen -A directory -- "$word") ) + fi + ;; + + # Complete with any number of directories + set|s) + COMPREPLY=( $(compgen -A directory -- "$word") ) + ;; + + # Complete with directories from PATH + remove|rm|r) + local -a promptarr + IFS=: read -a promptarr < <(printf '%s\n' "$PATH") + local part + for part in "${promptarr[@]}" ; do + if [[ $part && $part == "$word"* ]] ; then + COMPREPLY=("${COMPREPLY[@]}" "$part") + fi + done + ;; + + # No completion + *) + return 1 + ;; + esac + fi +} +complete -F _path path -- cgit v1.2.3