diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | bash/bashrc.d/completion.bash | 49 |
2 files changed, 50 insertions, 0 deletions
@@ -1,6 +1,7 @@ bash/bashrc.d/* !bash/bashrc.d/aliases.bash !bash/bashrc.d/cd.bash +!bash/bashrc.d/completion.bash !bash/bashrc.d/grep.bash !bash/bashrc.d/ls.bash !bash/bashrc.d/options.bash diff --git a/bash/bashrc.d/completion.bash b/bash/bashrc.d/completion.bash new file mode 100644 index 00000000..d51557ae --- /dev/null +++ b/bash/bashrc.d/completion.bash @@ -0,0 +1,49 @@ +# builtin with builtins +complete -b builtin + +# cd/pushd with directories +complete -d cd pushd + +# command/hash/type with commands +complete -c command hash type + +# set with options +complete -A setopt set + +# shopt with shell options +complete -A shopt shopt + +# unset with shell variables and functions +complete -v -A function unset + +# ssh/sftp/ssh-copy-id with config hostnames +_ssh() { + local word config hosts option value + word=${COMP_WORDS[COMP_CWORD]} + config=$HOME/.ssh/config + hosts=() + + # Bail if the configuration file is illegible + if [[ ! -r $config ]]; then + return 1 + fi + + # Read hostnames from the file, no asterisks + while read -r option value _; do + if [[ $option == Host && $value != *'*'* ]]; then + hosts[${#hosts[@]}]=$value + fi + done < "$config" + + # Generate completion reply + COMPREPLY=( $(compgen -W "${hosts[*]}" -- "$word") ) +} +complete -F _ssh ssh sftp ssh-copy-id + +# scp/rsync with local files and hostnames (colon suffixes) +_scp() { + _ssh + COMPREPLY=( "${COMPREPLY[@]/%/:}" ) +} +complete -f -F _scp scp + |