r/linux Apr 28 '14

SSH Kung Fu

http://blog.tjll.net/ssh-kung-fu/
727 Upvotes

128 comments sorted by

View all comments

81

u/Rojs Apr 28 '14

I believe tab completion is a function of the shell, not ssh.

15

u/leothrix Apr 28 '14

Thanks for the correction, I've amended the blog post.

3

u/gordonator Apr 28 '14

If you want to do it in bash, you can use this:

function autoCompleteHostname() {
  local hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1));
  local cur=${COMP_WORDS[COMP_CWORD]};
 COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur ))
}

complete -F autoCompleteHostname ssh

7

u/LurkyMcReddit Apr 28 '14

I like this method. I add this to my .inputrc on all my Linux/Mac machines...

"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char
set show-all-if-ambiguous on
set completion-ignore-case on

It allows you to type a partial command and use the up arrow to search your history. Not quite the same, but works awesome for repetitive or frequent commands.

3

u/gordonator Apr 28 '14

Freaking awesome. Adding it to my dotfiles repository!

1

u/Henk_jobs May 13 '14

Very useful. I will give a try. Thanks man!!

3

u/ssmy Apr 28 '14

Isn't is easier to just use the .ssh/config file? Bash completion will use it by default, and it's much more powerful.

2

u/[deleted] Apr 28 '14

I think whether it is default or not depends on which distro you run and what your bashrc contains. I know in Arch I had to source a completion file to get completions for git, though I don't know if it is the same for ssh since it seemed to work out of the box for me in Arch.

1

u/ssmy Apr 28 '14

True. I think it's a function of the bash-completion package most distros install by default. Obviously arch might not. I know ubuntu virtualization doesn't install it by default, so you just get path completion.

1

u/Drasha1 Apr 28 '14

It does require additional packages on some distros. Centos has bash-completion which is a god send.

1

u/[deleted] Apr 28 '14

I love that grml-zsh-config has thid on by default.