如何不影响其他完成脚本重置COMP_WORDBREAKS? [英] How to reset COMP_WORDBREAKS without effecting other completion script?
问题描述
有是,当我实现一个bash自动完成功能的东西迷惑了我,我会把它放在 /etc/bash_completion.d /
为了实现一些功能,我想删除单词间隔符的冒号(:
)的变量 $ COMP_WORDBREAKS
并添加一个斜杠( /
)在开始的 $ COMP_WORDBREAKS
。
COMP_WORDBREAKS =/'>< =; |及(
_mytool()
{
当地CUR = $ {COMP_WORDS [COMP_CWORD]}
compopt -o无空间 #我在这里实现 COM prePLY =($(compgen .......... my_implement ......... - $ CUR))
}
完成-F _mytool mytool
不过,我不能复位 COMP_WORDBREAKS
直接,因为该值与其他完成脚本共享。通过完成函数被调用时,变量 COMP_WORDS
阵列已经被填充,所以局部改变 COMP_WORDBREAKS
没有任何效果
是否有一个剧本完成改变 COMP_WORDBREAKS
的任何解决方案,并与其他脚本没有影响?
$回声$ BASH_VERSION
4.2.10(1)-release
修改 $ COMP_WORDBREAKS
在完成脚本不推荐的方式(因为它是一个全局变量它可能会影响其他完成脚本的行为 - 例如SSH)
不过,bash补全提供,你可以用它来实现自己的目标的一些辅助方法。
要处理非断字字符完成的话推荐的方法是利用两个辅助方法:
-
_get_comp_words_by_ref
与-n排除
选项- 得到词到完全不考虑字符作为排除断字
-
__ ltrim_colon_completions
- 删除包含preFIX冒号COM prePLY项目
(为 http://tiswww.case.edu/php/chet/bash/FAQ 一种变通方法 - E13)
- 删除包含preFIX冒号COM prePLY项目
所以,这里是如何处理一个冒号一个基本的例子(:)在完成的话:
_mytool()
{
当地CUR
_get_comp_words_by_ref -n:CUR #我在这里实现 COM prePLY =($(compgen .......... my_implement ......... - $ CUR)) __ltrim_colon_completions$ CUR
}
完成-F _mytool mytool
作为最后一个提示,辅助方法位于的/ etc / bash_completion
。看看里面读取每个方法的详细描述和发现更多的helper方法。
There is something confuse me when I implement a bash auto-completion function which I'll put it in /etc/bash_completion.d/
In order to achieve some feature, I want to remove the word break characters colon (:
) from variable $COMP_WORDBREAKS
and add a slash (/
) at begin of $COMP_WORDBREAKS
.
COMP_WORDBREAKS=" /'><=;|&("
_mytool()
{
local cur=${COMP_WORDS[COMP_CWORD]}
compopt -o nospace
# my implement here
COMPREPLY=( $(compgen ..........my_implement......... -- $cur) )
}
complete -F _mytool mytool
However, I can't reset COMP_WORDBREAKS
directly because the value is shared with other completion scripts. By the time the completion function gets called, variable COMP_WORDS
array has already been populated, so locally changes COMP_WORDBREAKS
have no effect.
Is there any solution for changing COMP_WORDBREAKS
in one completion script and no effect to other script?
$ echo $BASH_VERSION
4.2.10(1)-release
Modifying $COMP_WORDBREAKS
in your completion script is not the recommended way (as it is a global variable and it could affect the behavior of other completion scripts - for example ssh).
However, bash completion offers some helper methods which you can use to achieve your goal.
The recommended way to handle non-word-breaking characters in completion words is by using the two helper methods:
_get_comp_words_by_ref
with the-n EXCLUDE
option
- gets the word-to-complete without considering the characters in EXCLUDE as word breaks
__ltrim_colon_completions
- removes colon containing prefix from COMPREPLY items
(a workaround for http://tiswww.case.edu/php/chet/bash/FAQ - E13)
- removes colon containing prefix from COMPREPLY items
So, here is a basic example of how to a handle a colon (:) in completion words:
_mytool()
{
local cur
_get_comp_words_by_ref -n : cur
# my implementation here
COMPREPLY=( $(compgen ..........my_implement......... -- $cur) )
__ltrim_colon_completions "$cur"
}
complete -F _mytool mytool
As a final tip, the helper methods are located in /etc/bash_completion
. Take a look inside to read a detailed description of each method and to discover more helper methods.
这篇关于如何不影响其他完成脚本重置COMP_WORDBREAKS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!