这可能比“自定义选项卡完成”有一个更好的名称,但这里的情况是:
通常,当我在命令行中input一个命令时,后跟{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
不过,我注意到至less有一个程序过滤这个列表: wine
。 考虑:
[user@host tmp]$ wine <TAB><TAB> chromatron2.exe foo.exe
它有效地将结果过滤到*.exe
。
认为它可能是一些负责过滤的包装脚本,做了一个和它的file
变成wine
不是一个脚本,而是一个可执行文件。
现在,我不知道这个“filter”是否在程序本身编码,或者在默认的wine安装过程中指定,所以我不知道这个问题是更适合的stackoverflow或超级用户,所以我穿过我的手指,扔在这里。 我很抱歉,如果我猜错了。 (另外,我也检查了一些类似的问题,但大多数都是不相关的,或者涉及编辑shellconfiguration。)
所以我的问题是,这个“过滤”是如何完成的? 提前致谢。
您可能会在您的系统上找到一个名为/etc/bash_completion
的文件,这个文件中充满了设置此行为的函数和complete
命令。 该文件将来自您的一个shell启动文件,如~/.bashrc
。
还可能有一个名为/etc/bash_completion.d
的目录,其中包含具有更多完成功能的单个文件。 这些文件来源于/etc/bash_completion
。
这就是我的系统上的/etc/bash_completion
wine
completion命令的样子:
complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine
这套文件大部分由Bash完成项目维护。
你可以看bash手册中的Programmable Completion来了解它是如何工作的。
我知道这是古老的,但我正在寻找与我自己的脚本类似的东西。
你可以玩一个我在这里做的例子: 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 ""