Programmable Autocompletion in Bash

I always knew Bash had programmable autocompletion but never knew what it was good for until I came across this snippet:

# tab completion for ssh hosts
SSH_COMPLETE=( $(cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | \
sed -e s/,.*//g | grep -v \| ) )
complete -o default -W “${SSH_COMPLETE[*]}” ssh

Throw that into your .bashrc. This will search your known hosts file for ssh hosts you’ve connected to so that when you hit tab after entering ssh, you have a list of hosts. Like tab autocomplete for ls, but for remote hosts. Neat! Now to think of other cool uses for this…

Leave a Reply