简体   繁体   English

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

[英]Can I make tab-completion filter files by extension?

Assume myprogram only consumes *.data files as command line arguments. 假设myprogram仅使用*.data文件作为命令行参数。 In terminal, when we do 在终端,当我们这样做

$ myprogram <tab>

we want only the *.data files to be listed for tab auto-complete. 我们只希望列出*.data文件以供选项卡自动完成。 How is this behavior achieved? 这种行为是如何实现的? The shell being used is Bash. 正在使用的shell是Bash。

Option 1 选项1

Type the following into your bash shell 在bash shell中键入以下内容

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

the -f option tells complete to only complete based on file names, not directories. -f选项告诉complete仅基于文件名而不是目录完成。 the -X option allows you to specify the filter pattern. -X选项允许您指定过滤器模式。

Option 2 选项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 ). 它会影响每个目录和每个命令的tab-completion(意思是像cdrm这样的东西,以及myprogram )。 It works by allowing you to specify file extensions that will not appear in tab-complete. 它的工作原理是允许您指定不会出现在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. 这不完全是您要求的,但如果您的工作目录中除了*.data之外没有多少文件,则排除所有选项不会太麻烦。 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 ~/.bash_profile文件中添加该行

FIGNORE=".py:.txt:.out:.exe:.c:<etc>"

The syntax there is to create a colon-separated list of the file extensions you want to ignore. 其语法是创建一个以冒号分隔的要忽略的文件扩展名列表。 After saving the new .bash_profile you must type . ~/.bash_profile 保存新的.bash_profile您必须键入. ~/.bash_profile . ~/.bash_profile for the changes you made to take effect. . ~/.bash_profile表示您所做的更改生效。

Further info 更多信息

For more info about the complete command check out Programmable Completion Builtins in the Bash manual. 有关完整命令的更多信息,请参阅Bash手册中的Programmable Completion Builtins

Better use _filedir instead of "raw" complete . 更好地使用_filedir而不是“raw” complete

Justification 理由

  • grep _filedir -r $(pkg-config --variable=completionsdir bash-completion) | wc -l
  • tilde ( ~ ) paths are being expanded 波浪号( ~ )路径正在扩展
  • Prefixes are removed for directories, ie for /home/tux/Do<TAB><TAB> , the list you get as a reply removes '/home/tux' and is thus much more compact 删除目录的前缀,即对于/home/tux/Do<TAB><TAB> ,作为回复获得的列表将删除'/ home / tux',因此更加紧凑
  • Easier to implement and more failsafe 更容易实现,更安全

MWE MWE

Write a file "completions/myprogram", which you source by . completions/myprogram 写一个文件“completions / myprogram”,你可以找到它. completions/myprogram . completions/myprogram . . completions/myprogram Content: 内容:

_myprogram()
{
    # init bash-completion's stuff
    _init_completion || return

    # fill COMPREPLY using bash-completion's routine
    _filedir '@(data)'
}
complete -F _myprogram myprogram

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

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