Bash auto-completion trick
Tuesday, July 31, 2012
zhs has a nice feature. If a program supports --help
then zsh can do auto-completion of its arguments. The trick is that zsh silently runs the program with --help
argument, parses the output and use it for auto-completion. I wish bash could do that. Well, not exactly but it can. Bash has auto-completions too using templates. But if you want to add auto-completion to your program you would need to write a new template and either put it to /etc/bash_completion.d/
or source in .bashrc
or .profile
.
But you don’t have to do any of it if you can re-use bash templates as is. For example the template for configure scripts does almost the same what zsh does for all programs. It simply parses the output from ./configure --help
. Unfortunately it works only for arguments that prefixed with double dash. Better than nothing anyways.