我可以通过扩展名制作制表符完成过滤器文件吗?

Yix*_*ing 6 unix bash command-line

假设myprogram仅将*.data文件作为命令行参数使用.在终端,当我们这样做

$ myprogram <tab>
Run Code Online (Sandbox Code Playgroud)

我们只希望*.data列出文件以供选项卡自动完成.这种行为是如何实现的?正在使用的shell是Bash.

Dse*_*sel 7

选项1

在bash shell中键入以下内容

complete -f -X '!*.data' myprogram

the -f option tells complete to only complete based on file names, not directories. the -X option allows you to specify the filter pattern.

Option 2

Note: This solution is global. It will affect tab-completion in every directory and on every command (meaning things like cd or rm, as well as myprogram). It works by allowing you to specify file extensions that will not appear in tab-complete. This is not exactly what you asked for, but if there aren't many files other than *.data in your working directory, excluding all the options won't be too much of a pain. For both these reasons this option is probably not what you want but it is still worth noting.

In the file ~/.bash_profile add the line

FIGNORE=".py:.txt:.out:.exe:.c:<etc>"
Run Code Online (Sandbox Code Playgroud)

其语法是创建一个以冒号分隔的要忽略的文件扩展名列表.保存新内容后,.bash_profile您必须键入. ~/.bash_profile要使更改生效的更改.

更多信息

有关完整命令的更多信息,请参阅Bash手册中的Programmable Completion Builtins.

  • 有关“ complete”命令的更多信息,请访问http://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html (2认同)
  • 你还想要什么 (2认同)