简体   繁体   English

创建制表符完成脚本。在一种情况下,我想使用完成另一个命令(例如git),是否可能?

[英]Creating a tab completion script. In one case I want to use the completion for another command (e.g. git), is it possible?

So I have some commands I am scripting up: 所以我有一些我正在编写脚本的命令:

all-build.sh all-git.sh all-build.sh all-git.sh

These run commands on multiple folders and spawn output windows (running tail -f on logs) and all sorts of other things. 这些命令运行在多个文件夹和生成输出窗口(在日志上运行tail -f)和各种其他事情。

However I they each take different commands and I want tab completion 但是,他们每个人都采取不同的命令,我希望标签完成

So I read a tutorial on tab completion and I have the all-completion.bash script as shown here: 所以我阅读了关于制表符完成的教程,我有all-completion.bash脚本,如下所示:

#!/usr/bin/env bash

complete -W "param1 param2 param3" all-build.sh
complete -W "status log push pull" all-git.sh

As you can see the all-build auto complete params are my own custom ones and I am happy with that. 正如你所看到的那样,所有构建的自动完成参数都是我自己的定制参数,我很满意。 However the git ones are a repeat of some of the actual git parameters. 然而,git是重复的一些实际的git参数。 So really what I want is something like: 所以我真正想要的是:

complete <use git completion> all-git.sh

So that when I do all-git submodule upd<tab> it auto completes to all-git submodule update using git's auto complete. 因此,当我执行all-git submodule upd<tab>它会使用git的auto complete自动完成all-git submodule update

Is that possible? 那可能吗?

We might use _completion_loader , (which is used to define completions on the fly ) in order to load git completions. 我们可能会使用_completion_loader ,(这是用来定义在飞行的完成)以加载git落成。

Then we might observe in official git-completion.sh file, that function used to complete git "submodules" is called _git , so we can simply pass it to complete via -F <function> option, see complete documentation . 然后我们可以在官方的git-completion.sh文件中观察到,用于完成git“子模块”的函数叫做_git ,所以我们可以简单地传递它来complete via -F <function>选项,参见完整的文档

So by writing 所以通过写作

source /usr/share/bash-completion/bash_completion
_completion_loader git

complete -F _git all-git.sh

we can achieve desired behaviour (if I got your issue correctly). 我们可以实现理想的行为(如果我正确地解决了您的问题)。

Note : That bash_completion script, might be located on your machine in different folder. 注意bash_completion脚本可能位于您的计算机上不同的文件夹中。 I've tested it on Ubuntu 16.04 with bash 4.3.48 . 我在Ubuntu 16.04上bash 4.3.48测试了它。

EDIT : In case this doesn't fully solve your issue, I believe this is the way you want to go :). 编辑 :如果这不能完全解决您的问题,我相信这是你想要的方式:)。 You can pretty much bend the solution for your needs. 您可以根据需要改进解决方案。

EDIT-2 : I've also assumed, you have already installed and working git completions. 编辑-2 :我也假设,你已经安装并运行git完成。

EDIT-3 : I've found a few interesting links, which are not solving your particular issue, but can give some light and deeper knowledge: 编辑3 :我发现了一些有趣的链接,这些链接并没有解决您的特定问题,但可以提供一些更轻松,更深入的知识:

It's very simple: 这很简单:

source /usr/share/bash-completion/completions/git
__git_complete all-git.sh __git_main

The __git_complete() function is a helper to create Git completions, it's used by Git itself like this: __git_complete git __git_main . __git_complete()函数是创建Git完成的辅助函数,它由Git本身使用,如下所示: __git_complete git __git_main You can use it for specific commands, like __git_complete all-submodule.sh _git_submodule . 您可以将它用于特定命令,例如__git_complete all-submodule.sh _git_submodule

It's specifically designed for this, so no need to complicate it more. 它是专门为此而设计的,因此无需进一步复杂化。

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

相关问题 Bash:以编程方式获取命令的完成输出(例如,在变量中) - Bash: Getting a command's completion output programmatically (e.g., in a variable) 如何使用一个命令的输出作为另一个命令的 bash 完成 - How to use the output of one command as the bash completion for another Cygwin git tab完成 - Cygwin git tab completion 任意命令完成 - 可能吗? - Arbitrary command completion - possible? 如何使Git别名的制表符完成行为像现有命令一样? - How can I make tab completion for my Git alias behave like an existing command? 如何在 Swift 脚本中运行终端命令? (例如 xcodebuild) - How do I run a terminal command in a Swift script? (e.g. xcodebuild) 如何将一个列表(例如2和3)上的数字与另一个列表中的近似值(例如5)进行匹配? - How to match numbers on one list (e.g. 2 and 3) with the approximate sum on another list (e.g. 5)? bash 完成脚本的令人困惑的情况 - confusing case of a bash completion script 如何使用制表符完成替换命令行参数? - How can I replace a command line argument with tab completion? 给定一个 logfile.txt,我只想提取 java 脚本名称; 例如使用 bash 脚本的 filename.js - Given a logfile.txt, I want to extract the java script name only; e.g. filename.js using a bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM