aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ssh.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2013-09-01 00:04:10 +1200
committerTom Ryder <tom@sanctum.geek.nz>2013-09-01 00:04:10 +1200
commitfeb7dd2c70c9a45878c094bb8f8c2c3b98400fee (patch)
treebc817ca60e3d59ecf0e323df166bcf58bb0d939b /bash/bashrc.d/ssh.bash
parentAdd command completion for sudo (diff)
downloaddotfiles-feb7dd2c70c9a45878c094bb8f8c2c3b98400fee.tar.gz
dotfiles-feb7dd2c70c9a45878c094bb8f8c2c3b98400fee.zip
Move SSH completion into its own file
Diffstat (limited to 'bash/bashrc.d/ssh.bash')
-rw-r--r--bash/bashrc.d/ssh.bash31
1 files changed, 31 insertions, 0 deletions
diff --git a/bash/bashrc.d/ssh.bash b/bash/bashrc.d/ssh.bash
new file mode 100644
index 00000000..fda88b30
--- /dev/null
+++ b/bash/bashrc.d/ssh.bash
@@ -0,0 +1,31 @@
+# Commpletion for 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
+
+# Completion for scp/rsync with local files and hostnames (colon suffixes)
+_scp() {
+ _ssh
+ COMPREPLY=( "${COMPREPLY[@]/%/:}" )
+}
+complete -f -F _scp scp
+