简体   繁体   English

具有文件路径完成功能的自定义Bash自动完成

[英]Custom Bash Autocomplete with File Path Completion

I'm working on bash auto-completion for a project I'm the maintainer of. 我正在为我维护的项目进行bash自动补全。 You can find the script here . 您可以在此处找到脚本 I've cobbled this together with some hacking on my own, and with the help of some contributors who understand that completion APIs better than I do. 我自己进行了一些修补工作,并在一些贡献者的帮助下完成了这些工作,这些贡献者比我更了解完成API。

What we have works great -- with one exception. 我们拥有的作品很棒-除了一个例外。 We can manage a completion like like this 我们可以像这样管理完成

//type
pestle.phar som[TAB]

//completes to 
pestle.phar some-command-name 

However, once we're here we lose file path/name completion that's a part of the stock bash shell. 但是,一旦到达此处,我们将丢失文件路径/名称的补全,这是stock bash shell的一部分。 That is, working off the previous example, if a user types 也就是说,如果用户键入

//type
pestle.phar some-command-name /va[TAB]

we'd like it to complete to 我们希望它完成

//completes to the following, because var exists
pestle.phar some-command-name /var

Is there a way to just tell the complete command something like 有没有办法告诉complete命令像

Hey, in addition to everything we're telling you to do with our custom bash function, also keep your normal file path completion 嘿,除了我们要告诉您的有关自定义bash函数的所有操作外,还请确保正常的文件路径完成

If not, is there there some known science/boilerplate to reimplementing the file path completion in your own custom base completion functions? 如果不是,是否有一些已知的科学/模板可以在您自己的自定义基本完成功能中重新实现文件路径完成?

Some other answers and the docs seem to indicate that the -o filenames or -o bashdefault options should take care of this -- but it doesn't seem to be working on OS X 10.11. 其他一些答案文档 似乎表明-o filenames-o bashdefault选项应解决此问题-但在OS X 10.11上似乎不起作用。 I'm not sure if I misunderstand -o , or if the code in my completion files somehow overrides the -o behavior, or if OS X is doing it's I'm only a mostly well behaved unix thing. 我不确定我是否误解了-o ,或者我的完成文件中的代码是否以某种方式覆盖了-o行为,或者OS X是否正在执行此操作, 所以我只是行为良好的unix事情。

Also -- if it's not obvious -- this is my first deep bash completion rodeo. 另外-如果不是很明显-这我的第一个深打击完成牛仔竞技表演。 If I've said something seemingly dumb/naive above please let me know. 如果我在上面说了些愚蠢/天真的话,请告诉我。 I may be looking for a fish right now, but I'd like to learn to fish in the bash completion river myself. 我现在可能在寻找一条鱼,但我想学习鱼在bash补河自己。

I think -o default (without -o filenames ) should work for you. 我认为-o default (不带-o filenames )应该适合您。 According to the manual: 根据手册:

  • bashdefault bashdefault
    Perform the rest of the default bash completions if the compspec generates no matches. 如果compspec没有生成匹配项,则执行其余的默认bash补全。
  • default 默认
    Use readline's default filename completion if the compspec generates no matches. 如果compspec没有生成匹配项,请使用readline的默认文件名完成
  • filenames 文件名
    Tell readline that the compspec generates filenames, so it can perform any filename-specific processing (like adding a slash to directory names, quoting special characters, or suppressing trailing spaces). 告诉readline compspec生成文件名,因此它可以执行任何特定于文件名的处理(例如,在目录名称中添加斜杠,引用特殊字符或抑制尾随空格)。 Intended to be used with shell functions. 打算与外壳函数一起使用。

(Also see 'complete -d -o default cd' issue for the difference between -o default and -o bashdefault .) (有关-o default-o bashdefault之间的区别,另请参见'complete -d -o default cd'问题 )。

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

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