bash 中的复杂键绑定

发布于 2024-12-19 08:26:43 字数 350 浏览 4 评论 0原文

有没有一种方法可以将两个操作组合成一个键绑定(不认为可以与函数一起使用)。

这就是我想做的:

我想要一个键绑定(例如 Ctrl-X)来

  1. 插入一些文本,然后
  2. 调用完整或菜单完整,使用插入的文本作为完成的基础

我知道我可以(在 ~/.inputrc 中)指定

  • 插入文本 (CX: "ls")
  • 执行 readline 命令 (C-SPACE: menu-complete< /code>)

但我不知道如何放置这些 一起

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

This is what I'd like to do:

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

  1. insert some text, then
  2. invoke the complete or menu-complete, using the inserted text as the basis for the completion

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

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

But I am not sure how to put these together

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2024-12-26 08:26:43

这样做的技巧是调用重新绑定键的函数。在我的示例中,我将使用 Cb 插入文本并调用菜单完成,而不是 Cx。在我的示例中,您必须牺牲一个密钥 Ct

在 .bashrc 中,或者要获取的 bash 文件

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

这是如何工作的:

使用 bind,您可以将密钥绑定到执行以下三件事之一,但不能组合使用:

  • 执行 readline 命令:bind '"key": command'
  • 执行一系列击键:bind '"key":"key Strikes"'
  • 执行 shell 命令:bind -x '"key": shell-command'

所以如果你想结合这三件事,你会需要将它们每个绑定到单独的击键组合(在我的示例中Ct{1,2,3})并绑定一个键来执行所有这些击键。

在示例中:

Cb 首先插入 ls 并“按下”Ct1,从而执行 set_Cb_to_complete< /code>,这又将 Cb 重新绑定到 menu-complete。它还重新绑定 Cm、回车符或 Enter,因为它现在需要做两件事:接受该行,并重置 Cb 以插入ls,通过调用 set_Cb_to_insert_text 函数,该函数还会将 Enter 重置为正常使用。

我之所以说 Ct 必须被“牺牲”,是因为如果你按 Ct,readline 会等待看看你是要按 1 还是 2,或任何绑定的按键序列,在执行任何操作之前。但是,当您第一次使用 Ct 进行此用途时,您可以将其用作大量击键的初始键,以覆盖所有 readline 技巧。

一条建议:当您编写和测试这些内容时,请将备用键绑定到accept-line,因为突然有东西在错误的位置中断了链条,并且您被困在终端中而无法进行操作。执行命令:)

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

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

How this works:

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

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

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.

In the example:

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.

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 :)

枕花眠 2024-12-26 08:26:43

这可能对您有用:

"\ex": menu-complete
"\ez": "ls \ex"

将这些行包含在您的 ~/.inputrc 文件中。

这些行将 Alt-x 设置为 menu-complete,将 Alt-z 设置为 ls space menu-complete。这将为您提供目录中的第一个文件,并使用 Alt-x 一次循环浏览剩余的文件。

请参阅此处了解更多宏示例。

通过调用 bind -pbind -P 来检查 readline 命令,bind -s 将显示您已有的宏。请参阅此处了解绑定命令,您也可以制作一个关闭宏,请参阅此处。最后检查 .inputrc 文件是否正在被读取,我遇到了麻烦,因为环境变量设置为 /etc/Inputrc 并且我的个人版本从未被调用。

顺便说一句,请避开 Control-x,因为它已经用于许多 readline 命令。

This might work for you:

"\ex": menu-complete
"\ez": "ls \ex"

Include these lines in your ~/.inputrc file.

These lines set Alt-x to menu-complete and Alt-z to ls space menu-complete. This will give you the first file in the directory and use Alt-x to cycle through the remainder one at a time.

See here for more examples of macros.

Checkout the readline commands by invoking bind -p or bind -P and bind -s will show the macros you already have. See here for the bind command also you can make one off macros too, see here. Lastly check that the .inputrc file is being read, I had trouble because the environmental variable was set to /etc/Inputrc and my personal version was never being invoked.

BTW steer clear of Control-x as it is already in use for many readline commands.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文