ZSH历史记录自动补全菜单

19

Zsh自动补全功能非常出色,但我无法正确地配置其中一项:我想让zsh从历史记录中为我提供一个命令列表。

我知道可以使用Ctrl+R来搜索历史记录,但我想要的略有不同。当我输入:

shelajev@elephant ~ » kill 1TAB
1642 shelajev gnome-keyring-d
1718 shelajev gnome-session
1807 shelajev ssh-agent
1810 shelajev dbus-launch
1811 shelajev dbus-daemon
1822 shelajev gnome-settings-
1884 shelajev gvfsd
1891 shelajev gvfs-fuse-daemo

Zsh会为我提供一个进程列表以便杀死进程。我想要类似于这样的东西:

shelajev@elephant ~ » kill Ctrl+X Ctrl+X
kill -9 12093
kill -15 4123

其中列表中的项目是从我的历史记录中获取的。

虽然存在ZLE hist-complete功能,但我不知道如何正确配置它。

在我的.zshrc中,我有以下内容:

zle -C hist-complete complete-word _generic
zstyle ':completion:hist-complete:*' completer _history
bindkey "^X^X" hist-complete

但这仅仅是针对单个单词的搜索,帮助有限。是否有一种方法可以结合历史记录中的搜索,并显示列表呢?

4个回答

19

在zsh中有一个叫做history-beginning-search-menu的东西。 如果你输入:

autoload -Uz history-beginning-search-menu
zle -N history-beginning-search-menu
bindkey '^X^X' history-beginning-search-menu

在你的.zshrc文件中添加以下内容,例如:

kent$  sudo systemctl[here I type C-X twice]
Enter digits:
01 sudo systemctl acpid.service                      11 sudo systemctl enable netfs
02 sudo systemctl enable acpid                       12 sudo systemctl enable networkmanager
03 sudo systemctl enable alsa                        13 sudo systemctl enable NetworkManager
04 sudo systemctl enable alsa-restore                14 sudo systemctl enable NetworkManager-wait-online
05 sudo systemctl enable alsa-store                  15 sudo systemctl enable ntpd
06 sudo systemctl enable cronie                      16 sudo systemctl enable sshd
07 sudo systemctl enable cups                        17 sudo systemctl enable syslog-ng
08 sudo systemctl enable dbus                        18 sudo systemctl enable tpfand
09 sudo systemctl enable gdm                         19 sudo systemctl reload gdm.service
10 sudo systemctl enable hal                         20 sudo systemctl restart gdm.service

那么您需要为历史记录命令提供索引号。

当然,这方面可能有一些优化。但我认为这可以帮助您入门。

希望能对您有所帮助。


谢谢,这正如你所说的一样有效。这是一个很好的起点!我只是想知道是否有一种方法可以通过类似“菜单选择”的方式来增强它。 - Oleg Šelajev
我也在寻找潜在的菜单选择+数字选择组合。@OlegŠelajev,你找到了吗? - Mikko Ohtamaa
还没有找到任何东西,但我没有深入研究过。 - Oleg Šelajev
Oleg @MikkoOhtamaa 请查看我的回答 - Marlon Richert

8

@marlon-richert 我尝试按照手动步骤(https://github.com/marlonrichert/zsh-autocomplete#manual-installation)进行操作,但是它没有起作用。我打开了我的iterm终端,但是没有得到任何建议。 - Badman
1
@Badman 这里不是讨论那个的合适场所。但欢迎你在我的 GitHub 存储库开启一个问题:https://github.com/marlonrichert/zsh-autocomplete/issues - Marlon Richert

2
如果你只想在当前行中自动完成顶部匹配项(例如 fish shell),可以尝试 zsh-autosuggestions。当你输入类似于历史命令的命令时,它会建议一个带有浅灰色文本的自动补全。你可以按 → 键接受自动补全建议。

zsh-autosuggestions


0
几年过去了,现在有一个名为zsh-navigation-tools的插件。它并不完全符合您的要求,但也许您或其他人会发现它很有用。然而,需要注意的是,它将默认的bck-i-search (CTRL+R)替换为全屏应用程序。
感谢Mike向我指出这个插件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接