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

use*_*342 7 bash terminal zsh iterm

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

Eri*_*ric 6

提到的人readline是对的。

\n\n

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

\n\n

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

\n\n
$ bind -P |grep C-r\nre-read-init-file can be found on "\\C-x\\C-r".\nreverse-search-history can be found on "\\C-r".\nrevert-line can be found on "\\M-\\C-r".\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
$ bind -q reverse-search-history\nreverse-search-history can be invoked via "\\C-r".\n
Run Code Online (Sandbox Code Playgroud)\n\n

man readline包括:

\n\n

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

\n\n

看起来不错。我们如何改变它?假设您想使用 \xe2\x8c\x98-B 代替。这就是\\M-breadline 中的 Meta-b(又名 )。

\n\n

让我们尝试一下:

\n\n
$ bind \'\\M-b:reverse-search-history\'\n$ bind -q reverse-search-history\nreverse-search-history can be invoked via "\\C-r", "\\M-b".\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在按 \xe2\x8c\x98-b 会像 Control-R 一样触发反向搜索。但 Control-R 仍处于绑定状态。我们可以解决这个问题:

\n\n
$ bind -r \'\\C-r\'\n$ bind -q reverse-search-history\nreverse-search-history can be invoked via "\\M-b".\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
$ echo \'"\\M-b": reverse-search-history\' >> ~/.inputrc\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n