简体   繁体   English

Bash终端中的多个制表符完成例程

[英]Multiple tab-completion routines in Bash terminal

Is it possible to have multiple tab-completion routines, triggered by different key combinations? 是否可以使用由不同组合键触发的多个制表符完成例程?

For example, I have a custom tab-completion function which parses the output of myprog -help to tab-complete options. 例如,我有一个自定义tab-completion函数,它将myprog -help的输出解析为tab-complete选项。 Is there some way I could keep the default tab-complete behaviour on Tab untouched, and bind my custom completion to something like Shift + Tab ? 有没有什么方法可以保持Tab上的默认选项卡完成行为不变,并将我的自定义完成绑定到像Shift + Tab这样的东西?

The keybindings themselves are part of readline, and can be changed in your ~/.inputrc or use the bind command in your .bashrc (etc.), but they don't bind directly to user-defined functions, only indirectly as macros that might call those functions. 键绑定本身是readline的一部分,可以在~/.inputrc更改或在.bashrc (等)中使用bind命令,但它们不直接绑定到用户定义的函数,只是间接绑定为宏可能会调用这些功能。

"\C-xhf": dump-functions        # for ~/.inputrc
bind '"\C-xhf":dump-functions'  # in .bashrc (etc.)

bind -l will show you builtin functions you can bind to. bind -l将显示您可以绑定的内置函数。 You can add macros this way, which let's you run any bash function you want, although not in completion context, so the results don't get stuffed back into the command line. 您可以通过这种方式添加宏,这样您就可以运行所需的任何bash函数,但不是在完成上下文中,因此结果不会被填充回命令行。 Example: 例:

"\C-xc":  "echo hi\n"       # note it's a just macro, not a direct binding.

I'm guessing you already know you can override completion with the complete builtin for commands at the left on the command line. 我猜你已经知道你可以使用命令行左边命令的complete内置来覆盖完成。 Normally I'd just suggest creating your own completion routines that way and using them instead of working out a way to call an alternate set. 通常情况下,我建议以这种方式创建自己的完成例程并使用它们,而不是设法调用备用集。 Example: 例:

_complete_ssh ()
{
    local ssharg="${COMP_WORDS[COMP_CWORD]}"
    local filter=cat
    case "$ssharg" in *@*) filter="sed s/^/${ssharg%@*}@/" ;; esac
    local host=${ssharg##*@}
    COMPREPLY=( $(compgen -W "`
        sed 's/[, ].*//'  ~/.ssh*/known_hosts{,2} | sort | uniq
    `" $host | $filter ))
    return 0
}
complete -F _complete_ssh ssh          # for any command starting with "ssh "...

I experimented with binding universal-argument to see if it would affect a function run via a macro ("C-xu" to prefix UA, then "C-xf" to run a function), but I haven't noticed anything to indicate the function can detect UA. 我尝试绑定universal-argument以查看它是否会影响通过宏运行的函数(“C-xu”为前缀UA,然后“C-xf”运行函数),但我没有注意到任何指示该功能可以检测UA。 You'd think bind would have a option to regurgitate what UA was for the previous command, or a $-something, which might have allowed a user-defined completer sub-function to detect UA from inside of complete , and thus behave differently, but I don't see any sign of the idea in the readline source code. 你认为bind可以选择反复UA对前一个命令的反应,或者一个$ -something,它可能允许用户定义的完成子函数从complete内部检测UA,因此表现不同,但我在readline源代码中没有看到任何关于这个想法的迹象。 There's no obvious way to see what keybinding evoked a function or macro either (I didn't expect one), to allow mapping two keybindings to the same complete builtin, then backtracking to see which keybinding triggered it. 没有明显的方法可以看到键绑定引发了一个函数或宏(我没想到一个),允许将两个键绑定映射到相同的complete内置,然后回溯以查看哪个键绑定触发了它。

Note that Shift-TAB would be hard to bind anyway, and impossible to type from an old-school terminal. 请注意,Shift-TAB无论如何都很难绑定,也无法从老式终端输入。 bash doesn't use the X libraries and can't see Shift directly as a modifier, instead depending on terminals or terminal emulators for input in some character set, which don't have an applicable concept of modifiers. bash不使用X库,不能直接看到Shift作为修饰符,而是根据终端或终端仿真器在某些字符集中输入,这些字符集没有适用的修饰符概念。

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

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