bash completion for certain types of files in a special directory
-
03-07-2019 - |
Question
I have a list of unison profiles which exists in ~/.unison/*.prf
.
I'd like to have bash completion so that when I type unison
or unison-gtk
and hit tab it would list the .prf
files from that folder without the .prf
part.
Perhaps an example would be clearer:
$ ls ~/.unison/*.prf
default.prf dot-mozilla.prf to-desktop.prf
$ cd ~ # just to show you don't have to be in the ~/.unison folder
$ unison to<tab>
$ unison to-desktop
I foresee needing this for another tool as well, so it would be convenient if there were parts that could be reused.
Solution
If you are running debian/ubuntu or possibly other GNU/Linux distros, there should be examples of this type of completion in your /etc/bash_completion.d/ directory. Here is an example of how you might set up a completion script for unison:
have unison &&
_unison_show()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($( compgen -W "$(for x in ~/.unison/*.prf; do echo $(basename ${x%.prf}); done)" -- $cur ) )
}
complete -F _unison_show unison
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow