在bash复杂的按键绑定 [英] Complex keybinding in bash

查看:123
本文介绍了在bash复杂的按键绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以两个操作组合成一个按键绑定(不认为会与功能的工作)。

Is there a way to combine two operations into one keybinding (dont think would work with function).

这是我想要做的:

我想一键绑定(按Ctrl说-X),以 -

I'd like a keybinding (say Ctrl-X) to -


  1. 插入一些文字,然后

  2. 调用完整或菜单完成,采用插入的文本为基础,完成

我知道我可以(在〜/ .inputrc文件)指定

I know that I can (in ~/.inputrc) specify


  • 文本的插入( C-X:LS

  • 执行readline的命令( C-SPACE:菜单完成

  • Insertion of text with (C-X: "ls")
  • Execute readline commands (C-SPACE: menu-complete)

但我不知道如何把这些结合在一起

But I am not sure how to put these together

推荐答案

诀窍是调用它重新绑定钥匙的功能。在我的例子,我将使用<大骨节病> C-B 插入文本,并呼吁,而不是<大骨节病> C-X 菜单完成。你必须牺牲一个关键,在我的例子<大骨节病> C-牛逼

The trick to this is to call functions which rebinds your keys. In my example I'll use C-b to insert text and to call menu-complete, instead of C-x. You'll have to sacrifice a key, in my example C-t

在.bashrc里,或一个bash文件被源

In .bashrc, or a bash file to be sourced

set_Cb_to_insert_text() {
  bind '"\C-m": accept-line'
  bind '"\C-b":"ls \C-t1"'
  bind -x '"\C-t1":set_Cb_to_complete'
}
set_Cb_to_complete() {
  bind '"\C-m":"\C-t2\C-t3"'
  bind '"\C-b": menu-complete'
  bind '"\C-t2": accept-line'
  bind -x '"\C-t3":set_Cb_to_insert_text'
}
set_Cb_to_insert_text

这是如何工作:

使用绑定,可以绑定按键做的三件事之一,但没有他们的组合:

With bind, you can bind keys to do one of three things, but no combination of them:


  • 执行一个readline的命令:绑定的钥匙:命令

  • 执行一系列的按键:绑定的钥匙:击键

  • 执行shell命令:绑定-x'钥匙:壳命令

  • Execute a readline command: bind '"key": command'
  • Execute a series of keystrokes: bind '"key":"keystrokes"'
  • Execute a shell command: bind -x '"key": shell-command'

所以,如果你想这三样东西结合起来,你需要他们每个绑定到按键的组合分开(在我的例子<大骨节病>的Ct {<大骨节病> 1 ,<大骨节病> 2 ,<大骨节病> 3 }),并结合一键执行所有这些按键。

So if you want to combine these three things, you'll need to bind them each to a separate combination of keystrokes (in my example C-t{1,2,3}) and bind a key to execute all these keystrokes.

在这个例子:

<大骨节病>的Cb 首先将 LS 和presses<大骨节病>的Ct <大骨节病> 1 ,它执行 set_Cb_to_complete ,这反过来又重新绑定<大骨节病>的Cb 菜单完成。它还重新绑定<大骨节病> CM ,回车或<大骨节病>输入,因为它现在需要做两件事:接受就行了,复位<大骨节病>的Cb 插入 LS ,通过调用 set_Cb_to_insert_text 的功能,这也将重置<大骨节病>输入到它的正常使用。

C-b first inserts ls and 'presses' C-t1, which executes set_Cb_to_complete, which in turn rebinds C-b to menu-complete. It also rebinds C-m, carriage return, or Enter, because it now needs to do two things: Accept the line, and reset C-b to insert ls, by calling the set_Cb_to_insert_text function, which also resets Enter to it's normal use.

我之所以这么说,<大骨节病>的Ct 已被牺牲是,如果你preSS <大骨节病>的Ct ,readline的等待,看看你要preSS 1,或2,或任何结合的键序列的,它需要的任何行动之前。但是,当你第一次已经把<大骨节病> C-牛逼此使用,您可以使用它作为一个初始密钥一个巨大的按键数量足以支付所有的readline挂羊头卖狗肉。

The reason I said that C-t had to be "sacrificed", is that if you press C-t, readline will wait to see if you are going to press 1, or 2, or any of the bound key sequences, before it takes any action. But when you first have put C-t to this use, you can use it as an initial key for a huge amount of keystrokes to cover all your readline trickery.

建议海贼王:当你正在编写和测试这些,绑定的备用钥匙接受行,因为突然的东西打破在错误的地方链,你滞留在终端没有一种方法来执行命令:)

Piece of advice: While you are writing and testing these, bind an alternate key to accept-line, because suddenly something breaks the chain at the wrong place, and you are stuck in a terminal without a way to execute commands :)

这篇关于在bash复杂的按键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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