You really can do bash completion with spaces!
A short period ago when I was working on my little ec2ssh project I ran into the issue of tab completion not working for things with spaces in them.
This was my first foray into the world of bash completion, so I incorrectly thought "this can't be that hard - everything has completion now."
After Googling for hours, trying various things, finding statements saying that it wasn't possible - I stumbled upon the solution. It works properly and was surprisingly easy.
The compgen
command typically works with spaces for the word separators. What I had to do was make sure the source data was separated by something else, such as a linebreak (\n) and then tell compgen
that was the separator.
So this line before the compgen
command was key:
local IFS=$'\n'
Next, the complete
command still won't work without feeding it the -o filenames
option. This seems to be critical, telling it to behave like we are dealing with filenames makes it work properly. Note that I couldn't even find the original example that led me to this solution right now. It's that undocumented.
complete -o filenames
If you'd like to see this put altogether, in a very easy to read, semi-documented autocomplete script, check it out here:
https://github.com/mike503/ec2ssh/blob/master/ec2ssh.bash_completion.sh