Linux 在 shell 中自定义制表符补全
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10942919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customize tab completion in shell
提问by jedwards
This may be have a better name than "custom tab completion", but here's the scenario:
这可能比“自定义选项卡完成”有更好的名称,但这是场景:
Typically when I'm at the command line and I enter a command, followed with {TAB} twice, I get a list of all files and subdirectories in the current directory. For example:
通常,当我在命令行中输入一个命令,然后按 {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
However, I noticed at least one program somehow filters this list: wine
. Consider:
但是,我注意到至少有一个程序以某种方式过滤了此列表:wine
. 考虑:
[user@host tmp]$ wine <TAB><TAB>
chromatron2.exe foo.exe
It effectively filters the results to *.exe
.
它有效地将结果过滤为*.exe
.
Thinking it might be some sort of wrapper script responsible for the filtering, a did a which
and file
an it turns out wine
is not a script but an executable.
想这可能是某种包装脚本负责过滤,一个做了which
和file
的事实证明wine
是没有剧本的,但一个可执行文件。
Now, I don't know whether this "filter" is somehow encoded in the program itself, or otherwise specified during the default wine install, so I'm not sure whether this question is more appropriate for stackoverflow or superuser, so I'm crossing my fingers and throwing it here. I apologize if I guessed wrong. (Also, I checked a few similar questions, but most were irrelevant or involved editing the shell configuration.)
现在,我不知道这个“过滤器”是在程序本身中以某种方式编码的,还是在默认的 wine 安装期间以其他方式指定的,所以我不确定这个问题是否更适合 stackoverflow 或超级用户,所以我交叉我的手指,把它扔在这里。如果我猜错了,我道歉。(此外,我检查了一些类似的问题,但大多数都无关紧要或涉及编辑 shell 配置。)
So my question is, how is this "filtering" accomplished? Thanks in advance.
所以我的问题是,这种“过滤”是如何完成的?提前致谢。
采纳答案by Paused until further notice.
You will likely find a file on your system called /etc/bash_completion
which is full of functions and complete
commands that set up this behavior. The file will be sourced by one of your shell startup files such as ~/.bashrc
.
您可能会在系统上找到一个名为的文件,/etc/bash_completion
其中complete
包含设置此行为的函数和命令。该文件将来自您的 shell 启动文件之一,例如~/.bashrc
.
There may also be a directory called /etc/bash_completion.d
which contains individual files with more completion functions. These files are sourced by /etc/bash_completion
.
也可能有一个名为的目录/etc/bash_completion.d
,其中包含具有更多完成功能的单个文件。这些文件来自/etc/bash_completion
.
This is what the wine
completion command looks like from the /etc/bash_completion
on my system:
这是我系统上的wine
完成命令的样子/etc/bash_completion
:
complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine
This set of files is in large part maintained by the Bash Completion Project.
这组文件在很大程度上由Bash Completion Project维护。
回答by Diego Torres Milano
You can take a look at Programmable Completionin bash manual to understand how it works.
您可以查看bash 手册中的Programmable Completion以了解其工作原理。
回答by AnandKumar Patel
I know this is old but I was looking to do something similar with a script of my own.
我知道这很旧,但我想用我自己的脚本做类似的事情。
You can play around with an example I made here: http://runnable.com/Uug-FAUPXc4hAADF/autocomplete-for-bash
您可以使用我在此处制作的示例:http: //runnable.com/Uug-FAUPXc4hAADF/autocomplete-for-bash
Pasted code from above:
从上面粘贴的代码:
# 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 ""