aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ssh.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2015-10-21 13:08:09 +1300
committerTom Ryder <tom@sanctum.geek.nz>2015-10-21 13:08:09 +1300
commitdcfc80aaa112498498d93bae3ae0eb2088244c47 (patch)
tree889854ba1c96f1c86465da1bbd5cc87f0c8d4b9f /bash/bashrc.d/ssh.bash
parentHandle spaces correctly in completions (diff)
downloaddotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.tar.gz
dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.zip
Tidy up completion considerably; no more compgen
* Remove all instances of compgen; for filename completion it's quite broken as it relies on implicit wordsplitting in array context, and doesn't have an option to print with a null delimiter; replaced with manual for/while loops instead * Add IFS= to while/read loops over filenames * Use "dirname/s" instead of "dir/s" variables to avoid keyword collisions and for clarity * Remove some unnecessary variables * Use shorter syntax for loop exit conditions * Move completion options into functions where applicable rather than having them on the completion definition itself
Diffstat (limited to 'bash/bashrc.d/ssh.bash')
-rw-r--r--bash/bashrc.d/ssh.bash25
1 files changed, 14 insertions, 11 deletions
diff --git a/bash/bashrc.d/ssh.bash b/bash/bashrc.d/ssh.bash
index ea3c70a6..223aace8 100644
--- a/bash/bashrc.d/ssh.bash
+++ b/bash/bashrc.d/ssh.bash
@@ -1,23 +1,26 @@
# Completion for ssh/sftp/ssh-copy-id with config hostnames
_ssh() {
- local word
- word=${COMP_WORDS[COMP_CWORD]}
+
+ # Use default completion if no matches
+ compopt -o default
# Read hostnames from existent config files, no asterisks
local -a hosts
local config option value
for config in "$HOME"/.ssh/config /etc/ssh/ssh_config ; do
- if [[ -e $config ]] ; then
- while read -r option value _ ; do
- if [[ $option == Host && $value != *'*'* ]] ; then
- hosts=("${hosts[@]}" "$value")
- fi
- done < "$config"
- fi
+ [[ -e $config ]] || continue
+ while read -r option value _ ; do
+ [[ $option == Host ]] || continue
+ [[ $value != *'*'* ]] || continue
+ hosts=("${hosts[@]}" "$value")
+ done < "$config"
done
# Generate completion reply
- COMPREPLY=( $(compgen -W "${hosts[*]}" -- "$word") )
+ for host in "${hosts[@]}" ; do
+ [[ $host == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
+ COMPREPLY=("${COMPREPLY[@]}" "$host")
+ done
}
-complete -F _ssh -o default ssh sftp ssh-copy-id
+complete -F _ssh ssh sftp ssh-copy-id