What is the problem ?
I mostly write a shell script for some trivial tasks, its saves lot of typing and hence time. Initially its kind of hard coded , works for exactly one thing, But as i start using more often, i tend to make it more flexible with command line arguments. Guess what after a while i come down to same amount of typing by typing everything as a argument or write a new script that calls it with parameters.
But with all of these i felt like it was not efficient because for most commands when i type the arguments , tend to auto-complete on TAB, definitely saved a lot of typing.
Whereas, when i write my own script , even though the parameters were same/similar, it would not auto-complete. It was obvious how will bash know what arguments should be suggested/auto-completed to.
I was curious to figure out how does it work for normal commands like ssh, which could auto complete even with '@' in between. So first i will tell the solution i did for myself and also explain how it
Solution :
On googling i figured out that i could add autocomplete for custom commands or scripts by adding a new script in /etc/bash_autocompete.d/* . ( To source it the effect immediately you can source).
These auto-complete scripts seems to do nothing much but , create an array of options that were possible and print it when requested for a auto-complete for a certain commands. There are some standard parameter types like host_names, users, etc. you don't have to do even that. "complete" is a command line utility which has in some in built capabilities to do that.
Well i only wanted to have hostname as parameters so , i could do that with a simple one line script put in /etc/bash_autocomplete.d/
in my case it looked like this :
I mostly write a shell script for some trivial tasks, its saves lot of typing and hence time. Initially its kind of hard coded , works for exactly one thing, But as i start using more often, i tend to make it more flexible with command line arguments. Guess what after a while i come down to same amount of typing by typing everything as a argument or write a new script that calls it with parameters.
But with all of these i felt like it was not efficient because for most commands when i type the arguments , tend to auto-complete on
Whereas, when i write my own script , even though the parameters were same/similar, it would not auto-complete. It was obvious how will bash know what arguments should be suggested/auto-completed to.
I was curious to figure out how does it work for normal commands like ssh, which could auto complete even with '@' in between. So first i will tell the solution i did for myself and also explain how it
Solution :
On googling i figured out that i could add autocomplete for custom commands or scripts by adding a new script in /etc/bash_autocompete.d/* . ( To source it the effect immediately you can source
These auto-complete scripts seems to do nothing much but , create an array of options that were possible and print it when requested for a auto-complete for a certain commands. There are some standard parameter types like host_names, users, etc. you don't have to do even that. "complete" is a command line utility which has in some in built capabilities to do that.
Well i only wanted to have hostname as parameters so , i could do that with a simple one line script put in /etc/bash_autocomplete.d/
compete -F _known_hosts <script/command>
in my case it looked like this :
compete -F _known_hosts ./login
How does it work already for installed commands/utilities?
In fedora 23 (Others should have a similar approach , but need ot check on that) which i am using, the pre-installed commands have autocompletion. It comes from the script located in "/usr/share/bash-completion/bash_completion". This gets loaded for every new bash session.
The call chain is :
.bashrc -> /etc/bashrc -> /etc/profile.d/bash_completion.sh -> /usr/share/bash-completion/bash_completion
Well this is only true for very basic commands. Each command has its own the auto complete script in /usr/share/bash-completion/.
Wow! that's a treasure. This should cover most of the kinds of arguments, just reusing this might actually make it easier.
Next we will see how does a newly installed command installs its auto-complete script. Sounds exciting?? Stay tuned!