Monday, July 23, 2012

Using SSH with hostname tab completion

I've always hated typing the fully qualified hostname for a server that I wanted to SSH into.  I just learned how to configure Ubuntu to use tab completion with the SSH command.  Just run the following command and you can start using tab completion (NOTE: this might require a restart of your terminal session):

$ complete -W "$(echo $(grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //'))" ssh


$ ssh [TAB]

See the following article for more details:
http://www.commandlinefu.com/commands/view/2759/ssh-autocomplete

Update:  I just realized this morning that there's one more thing you need to do to get this working properly.  Somehow you need to source your custom complete commands when you open a new terminal.  I use bash, so I vi'd my .bashrc file and added my custom completion commands.  There's also a directory called /etc/bash_completion.d for these scripts, but I felt a little squeamish putting my custom ones there.


# enable programmable completion features (you don't need to 
enable this, if it's already enabled in /etc/bash.bashrc 
# and /etc/profile sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion


    # custom completions
    complete -W "$(echo $(grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //'))" ssh
fi

No comments: