Ubuntu – How to you mirror an autocomplete function in your own aliases (in 14.04)

14.04aliasauto-completionbash

I have a bunch of aliases that I've used on my 12.04 machine.

example:

alias pssh="tsocks ssh"
alias pscp="tsocks scp"
alias prdp="tsocks xfreerdp  ..........SOMETHING ...."

These aliases are modifications of regular linux commands (scp,ssh ect.) therefore I'd like these aliases to have the same autocomplete rules as their original counterparts.
I don't want to write my own rules as the rules for the regular commands are perfectly fine.

In 12.04 the following worked perfectly:

complete -F _ssh pssh
complete -F _scp pscp
complete -F _ssh prdp

This doesn't work in 14.04. If I try to autocomplete I get the following error:

bash: completion: function `_ssh' not found

If I autocomplete on ssh before I try pssh everything works. As far as I know, it's because the autocomplete rules are not loaded before they are used. Until the autocomplete rules for ssh gets loaded, my rules are invalid and result in an error.

How to I fix this, how do I correctly hook my alias into an autocomplete rule defined for a regular linux command?

Best Answer

Newer bash completion releases (>=2.0) improved hugely the time used to load the autocompletion logic. They made them by loading on request the required files, the newer location for these files is:

/usr/share/bash-completion/completions/

If you use ls -lah to explore such directory you'll find than there exist many soft links. That's because a file is needed for every command/alias, so, if you have pssh, pscp and prdp you'll need to create links with those names:

sudo ln -s /usr/share/bash-completion/completions/ssh /usr/share/bash-completion/completions/psss
sudo ln -s /usr/share/bash-completion/completions/ssh /usr/share/bash-completion/completions/pscp
sudo ln -s /usr/share/bash-completion/completions/ssh /usr/share/bash-completion/completions/prdp

Another alternative is to syslink the ssh autocompletion file to /etc/bash_completion.d/

sudo ln -s /usr/share/bash-completion/completions/ssh /etc/bash_completion.d/ssh

The former method will load the _ssh function on every single bash interactive session (making slower the startup).

After using any of the above methods your complete -F _ssh pssh ... configuration will relate the _ssh function with your aliases.