Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable bash auto-completion for a function?

I have read this tutorial on bash auto-completion An introduction to bash completion and I am trying to get the same auto-completion that is already done for ssh for one of my functions (that is loaded from .profile); which acts pretty much as an alias.

What I am trying to do is : get the same auto-completion, that is provided by default for ssh (which is the function _known_hosts; do complete -p | grep ssh and you will get complete -F _known_hosts /etc/init.d/ssh), and get it for my own function (which is installed like you woul install an alias, and that in fact does a scp and then an ssh with the original argument)

like image 553
Belun Avatar asked Jun 26 '15 11:06

Belun


People also ask

How do you auto complete in Linux?

Command auto-completion is available when using the CLI shell. You can set up CLI command auto-completion in your operating system using the kollacli command command. This command generates a script you can save to the operating system. Copy and paste the output to a file, for example a file named /etc/bash_completion.

Does bash have tab completion?

Bourne shell and csh do not, but ksh, bash, tcsh, and zsh all have tab completion to varying degrees. The basic principle in all of these shells is the same; you type the start of the word, hit the <TAB> key twice, and the list of possible commands or files is displayed.

How do I know if bash completion is installed?

Depending on your package manager, you have to manually source this file in your ~/. bashrc file. Reload your shell and verify that bash-completion is correctly installed by typing type _init_completion .


1 Answers

The completion function for ssh here is _ssh.

You can see this with complete -p ssh (it should also have been in your grep output) though it appears to be auto-loaded and so will not show up until after you have used it once in that session.

Anyway, that being said you should just be able to hook _ssh up to your function as well I would think.

complete -F _ssh myfunc
like image 92
Etan Reisner Avatar answered Oct 06 '22 18:10

Etan Reisner