如何将 iTerm 中的反向搜索 (Ctrl+R) 键盘映射到不同的组合键

How to keymap reverse-search (Ctrl+R) in iTerm to a different key combination

我正在尝试将反向搜索历史 (Ctrl+R) 命令映射到 iTerm 中的不同命令组合,但不确定如何操作?任何帮助将不胜感激。

提到readline的人是正确的。

您可以使用 bind 命令编辑您的绑定(尝试 help bind)。

对于这个特定的问题,让我们看看绑定到 Control-R 的是什么:

$ bind -P |grep C-r
re-read-init-file can be found on "\C-x\C-r".
reverse-search-history can be found on "\C-r".
revert-line can be found on "\M-\C-r".

好的。其中之一就是 \C-r,表示 Control-R。让我们仔细检查一下:

$ bind -q reverse-search-history
reverse-search-history can be invoked via "\C-r".

man readline 包括:

reverse-search-history (C-r) Search backward starting at the current line and moving `up' through the history as necessary. This is an incremental search.

看起来不错。我们如何改变它?假设您想改用 ⌘-B。这就是 readline-speak 中的 Meta-b(又名 \M-b)。

让我们试试看:

$ bind '\M-b:reverse-search-history'
$ bind -q reverse-search-history
reverse-search-history can be invoked via "\C-r", "\M-b".

现在按 ⌘-b 会像 Control-R 一样触发反向搜索。 Control-R 仍然是绑定的。我们可以解决这个问题:

$ bind -r '\C-r'
$ bind -q reverse-search-history
reverse-search-history can be invoked via "\M-b".

此更改将适用于当前 shell 会话,但会在下次调用 shell 时消失。要使更改持久化,请执行以下操作:

$ echo '"\M-b": reverse-search-history' >> ~/.inputrc

现在 ~/.inputrc 包含所需的绑定。任何使用它进行 readline 配置的程序(包括您的 shell)现在都将使用您指定的绑定。