获取compgen寻找文件时,列入目录的斜杠 [英] Getting compgen to include slashes on directories when looking for files
问题描述
我想从我的自定义完成后得到以下行为
I'd like to get the following behavior from my custom completion
由于
$ mkdir foo
$ touch foo faz/bar faz/baz
我想获得这个
$ foo -u <tab><tab> =>
foo faz/
$ foo -u fa<tab><tab> =>
foo -u faz/
$ foo -u faz/<tab><tab> =>
bar baz
我认为 compgen -ff
将输出富FAZ /
,但它输出富FAZ
这并不能帮助我很多。
I assumed that compgen -f f
would output foo faz/
, but it outputs foo faz
which doesn't help me much.
我是否需要后处理的输出或者是有选项一些魔法组合compgen工作?
Do I need to post-process the output or is there some magic combination of options to compgen that work?
推荐答案
我遇到了同样的问题。下面是我使用的解决方法:
I ran into the same problem. Here's the workaround I'm using:
- 注册的完成函数
-o默认
,例如完整-o默认-F _my_completion
。 - 当你想完成一个文件名,只需设置
COM prePLY =()
,让Readline的接管(这是什么- Ø默认
一样)。
- Register the completion function with
-o default
, e.g.,complete -o default -F _my_completion
. - When you want to complete a filename, just set
COMPREPLY=()
and let Readline take over (that's what the-o default
does).
有与这个潜在的问题 - 你可能会使用 COM prePLY =()
拒绝完成它是不恰当的,现在不会工作了。在bash 4.0以上,就可以解决这个使用 compopt
如下:
There's a potential problem with this -- you might be using COMPREPLY=()
to deny completion where it's not appropriate, and now that won't work anymore. In Bash 4.0 and above, you can work around this using compopt
as follows:
- 在您完成功能的顶部,始终运行
compopt + O默认
。这将禁用的ReadLine文件名完成时,COM prePLY
是空的。 - 当你想完成一个文件名,使用
compopt -o默认; COM prePLY =()
。换句话说,让Readline的文件名完成,只有当你需要它。
- At the top of your completion function, always run
compopt +o default
. This disables Readline filename completion whenCOMPREPLY
is empty. - When you want to complete a filename, use
compopt -o default; COMPREPLY=()
. In other words, enable Readline filename completion only when you need it.
我还没有想出了pre-4.0 Bash的一个完整的解决办法,但我有一些很好的作品对我来说足够。我可以形容这是否有人真正关心,但希望这些旧的Bash版本很快就会出来共同使用无论如何。
I haven't figured out a full workaround for pre-4.0 Bash, but I have something that works well enough for me. I can describe this if anyone really cares, but hopefully these older Bash versions will soon fall out of common use anyway.
这篇关于获取compgen寻找文件时,列入目录的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!