ZSH history completion menu
Asked Answered
I

4

19

Zsh autocomplete is amazing, but I cannot configure one thing properly: I want zsh to offer me a list of commands from history.

I know that I can search the history with Ctrl+R, but I want something a bit different. When I type:

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 offers me a list of processes to kill. I want to have something like this:

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

where those items in the list are taken from my history.

There exists a ZLE hist-complete thing, but I don't know how to properly configure it.

I have the following in my .zshrc:

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

But this only completes individual words, which doesn't give me much. Is there a way to combine history search for lines in the history and showing a listing of that?

Intisar answered 28/11, 2012 at 20:20 Comment(0)
A
19

there is something in zsh called history-beginning-search-menu. if you put:

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

in your .zshrc file. then for example:

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

then you need to give the index number to fire the command in history.

of course there could be some optimization for that. but I think this gets you start.

hope it helps.

Abelmosk answered 28/11, 2012 at 21:33 Comment(4)
Thanks, this works exactly as you said. It's a great starting point! I just wonder if there's a way to enhance this with something like menu select.Abnegate
I am also looking for potential menu select + digit select combination. @OlegŠelajev did you find any?Doggish
Haven't found anything, but I didn't look into it much.Abnegate
Oleg @MikkoOhtamaa See my answer below. 🙂Homeroom
H
8

My Zsh Autocomplete plugin includes a proper history menu, plus real-time multi-line history search. Try it out and let me know what you think!

live history search

history menu with multi-select

Homeroom answered 26/12, 2020 at 22:20 Comment(3)
@marlon-richert I tried to follow manual steps (github.com/marlonrichert/zsh-autocomplete#manual-installation) but it is not working. I open my iterm terminal but not getting any suggestion.Showthrough
@Showthrough This is not the right place to discuss that. Feel free to open an issue at my GitHub repo, though: github.com/marlonrichert/zsh-autocomplete/issuesHomeroom
how to invoke the history list, and how to select from the history list?Chirk
A
2

If you just want the top match auto-completed in the current line (like fish shell), try zsh-autosuggestions. When you type a command similar to a command in history, it suggests a completion with light-grey text. You can press → to accept the completion.

zsh-autosuggestions

Anchoveta answered 16/2, 2022 at 22:19 Comment(0)
T
0

A couple of years have passed and now there's a plugin called zsh-navigation-tools. It doesn't exactly do what you were looking for, but maybe you or someone else might find it helpful. However, it should be noted that it replaces the default bck-i-search (CTRL+R) by a fullscreen application.

Credits to Mike for pointing me to this plugin.

Tuning answered 1/11, 2018 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.