庆典 - 完成 - 完成函数中定义的第一次调用命令 [英] bash-completion - completion function defined the first time a command is invoked

查看:134
本文介绍了庆典 - 完成 - 完成函数中定义的第一次调用命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个新的别名 scp_using_rsync ,它使用rsync将在某些选项SSH复制文件。我想在bash完成对SCP连结到这个别名。

I've added a new alias scp_using_rsync, which uses rsync to copy files over SSH with certain options. I wanted to link the bash completion for scp to this alias.

它的工作原理,当我加入这一行:

It works when I add this line:

complete -o bashdefault -o default -o nospace -F _scp scp_using_rsync 2>/dev/null || complete -o default -o nospace -F _scp scp_using_rsync

唯一的问题是,我注意到, _scp 获取我的bash环境中定义,只有当我尝试在外壳至少一次Tab键完成的SSH / SCP。所以,如果我直接在一个新的shell中运行 scp_using_rsync ,我会得到 _scp 找不到错误。​​

The only problem is that I notice, _scp gets defined in my bash environment, only after I try tab-completion with ssh/scp at least once in that shell. So if I directly run scp_using_rsync in a new shell, I would get the _scp not found error.

输出排版-F 之前一个新的外壳,并试图为ssh或scp命令选项卡完成后,清楚地表明了以下功能在尝试制表完成后确定首次

The output from typeset -F in a new shell before and after trying tab completion for ssh or scp commands indicate clearly that the following functions get defined after trying tab-completion for the first time:

$ diff ~/.scratch/file1 ~/.scratch/file2
224a225,227
> declare -f _scp
> declare -f _scp_local_files
> declare -f _scp_remote_files
226a230
> declare -f _sftp
230a235,240
> declare -f _ssh
> declare -f _ssh_ciphers
> declare -f _ssh_macs
> declare -f _ssh_options
> declare -f _ssh_suboption
> declare -f _ssh_suboption_check

这些功能似乎在的/ usr /共享/ bash的自动完成/完井/ SSH 在我的系统中定义。

这是我的2相互关联的问题:

These are my 2 inter-related questions:


  • 如何bash中找出其中自动拿起定义,并定义它们在完成被试的第一次?

  • 我应该如何以类似的方式进行连接与bash完成了 scp_using_rsync 来SCP的bash补全?

  • How does bash figure out where to pick up the definitions automatically and define them when the completion is tried for the first time ?
  • How should I be linking the bash-completion for scp_using_rsync to scp's bash completion in a similar way ?

推荐答案

猛砸4.1 添加新的 -D 选项​​完整 compgen compopt

Bash 4.1 added a new -D option for complete, compgen and compopt:

新的完整/ compgen / compopt -D选项来定义一个`默认'完成:
      完成有关命令调用对于没有完成已
      定义
。如果这个函数返回的 124 ,可编程完成是
      再次尝试,允许用户动态地建立了一套完井
      作为完成由具有默认完成函数试图
      安装完成个别功能每次调用的时间。

New complete/compgen/compopt -D option to define a `default' completion: a completion to be invoked on command for which no completion has been defined. If this function returns 124, programmable completion is attempted again, allowing a user to dynamically build a set of completions as completion is attempted by having the default completion function install individual completion functions each time it is invoked.

还有从bash的手册的例子:

There's an example from bash's manual:

_completion_loader()
{
     . "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return 124
}
complete -D -F _completion_loader

这篇关于庆典 - 完成 - 完成函数中定义的第一次调用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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