在Bash脚本中查找基于文件名的自动完成功能 [英] find based filename autocomplete in Bash script

查看:68
本文介绍了在Bash脚本中查找基于文件名的自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直希望有一个命令行功能,我一直在考虑如何最好地实现它,但我一无所获...

There is a command line feature I've been wanting for a long time, and I've thought about how to best realize it, but I got nothing...

所以我想要的是当我开始输入文件名并点击选项卡时,例如:

So what I'd like to have is when I start typing a filename and hit tab,for example:

# git add Foo<tab>

我希望它运行find . -name "*$1*"并基本上自动将匹配文件的完整路径自动完成到我的命令行.

I'd like it to run a find . -name "*$1*" and basically autocomplete the complete path to the matched File to my command line.

我到目前为止所拥有的:

我知道我必须编写一个函数,该函数将使用我想要的参数来调用应用程序, 例如git add.之后,它需要捕获tab击键事件并进行上述查找,如果显示很多,则显示结果,如果显示,则填写结果.

I know I'll have to write a function that will call the app with the parameters I want, for example git add. After that it needs to catch the tab-keystroke event and do the find mentioned above, and display the results if many, or fill in the result if one.

我无法弄清的内容:

如何在函数内的函数中捕获Tab键事件.

How to catch the tab key event within a function within function.

所以基本上是伪代码:

gadd() {git add autocomplete_file_search($1)}

autocomplete_file_search(keyword) {
  if( tab-key-pressed ){
    files = find . -name "*$1*";
    if( filecount > 1 ) {
      show list;
    }
    if( files == 1 ) {
      return files
    }
  }
}

有什么想法吗?

谢谢.

推荐答案

您应该看一下 bash完成简介.简而言之,bash有一个用于配置和扩展制表符完成的系统.其他外壳也可以这样做,并且每个外壳都有不同的设置方式.使用该系统,您不必自己做任何事情,并且将自定义参数完成添加到命令中相对容易.

You should take a look at this introduction to bash completion. Briefly, bash has a system for configuring and extending tab completion. Other shells do this, too, and each one has a different way to set it up. Using this system it is not necessary to do everything yourself and adding custom argument completion to a command is relatively easy.

这篇关于在Bash脚本中查找基于文件名的自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆