简体   繁体   English

在shell中自定义选项卡完成

[英]Customize tab completion in shell

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. 通常,当我在命令行并输入命令,然后输入{TAB}两次时,我会获得当前目录中所有文件和子目录的列表。 For example: 例如:

[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 . 但是,我注意到至少有一个程序以某种方式过滤了这个列表: wine Consider: 考虑:

[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. 想这可能是某种负责过滤包装脚本的,一个做了whichfile的事实证明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. 现在,我不知道这个“过滤器”是否以某种方式编码在程序本身中,或者在默认的wine安装过程中以其他方式指定,所以我不确定这个问题是否更适合stackoverflow或超级用户,所以我是我的手指交叉扔在这里。 I apologize if I guessed wrong. 如果我猜错了,我道歉。 (Also, I checked a few similar questions, but most were irrelevant or involved editing the shell configuration.) (另外,我检查了一些类似的问题,但大多数都是无关紧要的,或者涉及编辑shell配置。)

So my question is, how is this "filtering" accomplished? 所以我的问题是,这种“过滤”是如何完成的? Thanks in advance. 提前致谢。

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. 您可能会在系统上找到一个名为/etc/bash_completion的文件,其中包含设置此行为的函数和complete命令。 The file will be sourced by one of your shell startup files such as ~/.bashrc . 该文件将由您的一个shell启动文件(如~/.bashrc

There may also be a directory called /etc/bash_completion.d which contains individual files with more completion functions. 可能还有一个名为/etc/bash_completion.d的目录,其中包含具有更多完成功能的单个文件。 These files are sourced by /etc/bash_completion . 这些文件来自/etc/bash_completion

This is what the wine completion command looks like from the /etc/bash_completion on my system: 这是wine completion命令从我的系统上的/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完成项目维护。

您可以在bash手册中查看Programmable Completion ,了解它的工作原理。

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 ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM