jed*_*rds 14 linux bash shell tab-completion bash-completion
这可能比"自定义标签完成"有更好的名称,但这是场景:
通常,当我在命令行并输入命令,然后输入{TAB}两次时,我会获得当前目录中所有文件和子目录的列表.例如:
[user@host tmp]$ cat <TAB><TAB>
chromatron2.exe Fedora-16-i686-Live-Desktop.iso isolate.py
favicon.ico foo.exe James_Gosling_Interview.mp3
Run Code Online (Sandbox Code Playgroud)
但是,我注意到至少有一个程序以某种方式过滤了这个列表:wine
.考虑:
[user@host tmp]$ wine <TAB><TAB>
chromatron2.exe foo.exe
Run Code Online (Sandbox Code Playgroud)
它有效地过滤了结果*.exe
.
认为它可能是负责过滤的某种包装脚本,a a which
和file
a结果wine
不是脚本而是可执行文件.
现在,我不知道这个"过滤器"是否以某种方式编码在程序本身中,或者在默认的wine安装过程中以其他方式指定,所以我不确定这个问题是否更适合stackoverflow或超级用户,所以我是我的手指交叉扔在这里.如果我猜错了,我道歉.(另外,我检查了一些类似的问题,但大多数都是无关紧要的,或者涉及编辑shell配置.)
所以我的问题是,这个"过滤"是如何完成的?提前致谢.
Pau*_*ce. 12
您可能会在系统上找到一个文件,/etc/bash_completion
其中complete
包含设置此行为的函数和命令.该文件将由您的一个shell启动文件提供,例如~/.bashrc
.
可能还有一个目录/etc/bash_completion.d
,其中包含具有更多完成功能的单个文件.这些文件来自/etc/bash_completion
.
这就是我的系统上的wine
完成命令的样子/etc/bash_completion
:
complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine
Run Code Online (Sandbox Code Playgroud)
这组文件在很大程度上由Bash完成项目维护.
我知道这已经过时了,但我正在寻找与我自己的剧本类似的东西.
你可以玩我在这里做的一个例子:http: //runnable.com/Uug-FAUPXc4hAADF/autocomplete-for-bash
粘贴上面的代码:
# Create function that will run when a certain phrase is typed in terminal
# and tab key is pressed twice
_math_complete()
{
# fill local variable with a list of completions
local COMPLETES="add sub mult div"
# you can fill this variable however you want. example:
# ./genMathArgs.sh > ./mathArgsList
# local COMPLETES=`cat ./mathArgsList`
# we put the completions into $COMPREPLY using compgen
COMPREPLY=( $(compgen -W "$COMPLETES" -- ${COMP_WORDS[COMP_CWORD]}) )
return 0
}
# get completions for command 'math' from function '_math_complete()'
complete -F _math_complete math
# print instructions
echo ""
echo "To test auto complete do the following:"
echo "Type math then press tab twice."
echo "You will see the list we created in COMPLETES"
echo ""
Run Code Online (Sandbox Code Playgroud)