Bash命令行选项卡完成冒号字符 [英] Bash Command-Line Tab Completion Colon Character
问题描述
当参数中不包含特殊字符时,我已经使用compgen机制为自定义命令成功实现了bash补全功能。
I have successfully implemented bash completions for my custom commands when the parameters contained no special characters in them, with the compgen mechanism:
current=${COMP_WORDS[COMP_CWORD]}
all=$(_get_all_items)
COMPREPLY=( $(compgen -W "$all" -- $current) )
...
complete -F _prog_compl prog
我使用相同的方法来完成以a开头的项目冒号::第一
,:第二
,...但无法显示/自动完成。我尝试用反斜杠转义冒号,但也不起作用。 我应该如何完全摆脱冒号?
I use the same approach to complete items which start with a colon character: :first
, :second
, ... But it fails to show me / autocomplete them. I tried escaping colons with backslashes which didn't work either. How should I escape colons in completion?
项目以冒号开头,例如::first
,:秒
。如果我写了progname并以这样的冒号开头:
The items are starting with colon, let's say: :first
, :second
. If I write progname and start with a colon like this:
$ progname :<Tab here after colon>
我看到没有完成,但有两个冒号( ::)-其中之一会自动添加到我键入的行中。如果我将冒号转换为普通字符(比方说'b'),则完全按照我的意愿完成操作: bfirst
, bsecond ...
I see no completion but two colons ("::") - one of them automatically added to the line I type. If I convert the colon to an ordinary character (let's say 'b') I get completion exactly like I want: bfirst
, bsecond
...
需要注意的是,当我按Tab时,它会在我之前插入的冒号旁边放置另一个:,并变成 ::
$ bash --version
GNU bash, version 4.1.10(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
另外,我在外壳中做了一些实验,得出:
Additionally, I have made some experiments in the shell which yield:
$ compgen -W ":aaaa5 :gb2 :cd3" -- ":"
:aaaa5
:gb2
:cd3
但是奇怪的是,在我唯一的:上放了另一个:并使其成为 ::
Yet it strangely puts another ":" to my sole ":" and makes it "::" after I type : on the command line.
$ complete
complete -F _alp_compl alp.sh
complete -o nospace -F __dbus_send dbus-send
complete -F _myprog_compl myprog
complete -o nospace -F __gdbus gdbus
complete -o nospace -F __gsettings gsettings
推荐答案
咨询 help-bash@gnu.org 后,我得到了答案:
After consulting help-bash@gnu.org I got an answer:
冒号会中断补全系统的单词(请查看COMP_WORDBREAKS shell变量的
描述),因此,当您键入
The colon breaks words for the completion system (look at the description of the COMP_WORDBREAKS shell variable), so when you type
程序名称时:[TAB]
progname :[TAB]
完成系统会显示一个空单词来完成。如果所有
可能的补全都以:作为最长的公共前缀,则
补全系统会将冒号插入行中。
the completion system gets an empty word to complete. If all of the possible completions have `:' as the longest common prefix, then the completion system will insert the colon into the line.
然后我的COMP_WORDBREAKS搜索从 stackoverflow 。
And my COMP_WORDBREAKS search yield me an answer from stackoverflow.
修复(我丢失了)来自 debian bash完成页面,现在,我的冒号启动的完成正在按我的意愿进行。
After fixing (my missing) /etc/bash_completion file from debian bash completion page, now, my colon-initiated completion is working as my will.
这篇关于Bash命令行选项卡完成冒号字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!