如何在不影响其他完成脚本的情况下重置COMP_WORDBREAKS? [英] How to reset COMP_WORDBREAKS without affecting other completion script?
问题描述
当我实现bash自动完成功能时,我会把它放在/etc/bash_completion.d/
There is something confuse me when I implement a bash auto-completion function which I'll put it in /etc/bash_completion.d/
为了实现某些功能,我想从变量$COMP_WORDBREAKS
中删除分词符冒号(:
),并在$COMP_WORDBREAKS
的开头添加斜杠(/
).
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
但是,我不能直接重置COMP_WORDBREAKS
,因为该值与其他完成脚本共享.到完成函数被调用时,变量COMP_WORDS
数组已经被填充,因此局部更改COMP_WORDBREAKS
无效.
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.
在一个完成脚本中更改COMP_WORDBREAKS
有什么解决方案,对其他脚本没有影响吗?
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
推荐答案
不建议在完成脚本中修改$COMP_WORDBREAKS
(因为它是全局变量,可能会影响其他完成脚本的行为,例如ssh).
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).
但是,bash完成提供了一些辅助方法,可用于实现目标.
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
和-n EXCLUDE
选项
- 获取完整单词,而无需将EXCLUDE中的字符视为分词
_get_comp_words_by_ref
with the-n EXCLUDE
option
- gets the word-to-complete without considering the characters in EXCLUDE as word breaks
- 从COMPREPLY项目中删除包含前缀的冒号
(针对 http://tiswww.case.edu/php/chet/bash/FAQ 的变通方法- E13)
- removes colon containing prefix from COMPREPLY items
(a workaround for http://tiswww.case.edu/php/chet/bash/FAQ - E13)
因此,这是一个如何用补全字处理冒号(:)的基本示例:
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
最后一个提示是,帮助程序方法位于
/etc/bash_completion
中.浏览内部,详细了解每种方法,并发现更多的辅助方法.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屋!