bash完成特殊目录中的某些类型的文件

Sco*_*ood 12 bash autocomplete

我有一个存在的unison配置文件列表~/.unison/*.prf.

我想要bash完成,以便当我键入unison或按unison-gtkTab键时,它将列出该.prf文件夹中没有该.prf部分的文件.

也许一个例子会更清楚:

$ 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
Run Code Online (Sandbox Code Playgroud)

我预见到另外一个工具也需要这个,所以如果有可以重复使用的部件会很方便.

vez*_*ult 16

如果您正在运行debian/ubuntu或可能还有其他GNU/Linux发行版,那么/etc/bash_completion.d/目录中应该有这种类型的完成示例.以下是如何为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
Run Code Online (Sandbox Code Playgroud)