zsh-users / zsh-autosuggestions

Fish-like autosuggestions for zsh
MIT License
30.95k stars 1.86k forks source link

Unable to clear autosuggestions #562

Open ljohnston opened 4 years ago

ljohnston commented 4 years ago

Describe the bug

I use zsh with vi mode enabled (bindkey -v). When I'm on the command line with an active suggestion and I enter vicmd mode, I want to clear and disable suggestions, but I can't seem to get it to work correctly. I'm pretty new to zsh so it's likely I'm misunderstanding how this is supposed to work.

To Reproduce

Steps to reproduce the behavior:

$ git clone --depth 1 --branch v0.6.4 https://github.com/zsh-users/zsh-autosuggestions.git 
$ zsh -df

Source the following minimal zshrc

source zsh-autosuggestions/zsh-autosuggestions.zsh

bindkey -v

autoload -Uz add-zle-hook-widget

zle_autosuggest_line_init() {
    zle autosuggest-enable
}

zle_autosuggest_keymap_select() {
    zle autosuggest-clear
    zle autosuggest-disable

    # Have also tried this.
    # zle vi-kill-eol

    # Uncomment to see this works, but something comes along later and redraws the command line.
    # sleep 3
}

# This doesn't work either.
# ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=("zle_autosuggest_keymap_select")

zle -N zle_autosuggest_line_init
zle -N zle_autosuggest_keymap_select
add-zle-hook-widget -Uz line-init zle_autosuggest_line_init
add-zle-hook-widget -Uz keymap-select zle_autosuggest_keymap_select

Then seed the suggestion, recall it, and hit escape:

$ ls /tmp
$ ls <escape>

The second ls above will add the /tmp suggestion as expected. When I hit ESC to go into vicmd mode, I'd expect the suggestion to be cleared and autosuggestions to be disabled. Indeed, autosuggestions do get disabled, but /tmp remains on the command line.

What's really strange, however, is if I uncomment the sleep 3, you can actually see that things are happening as expected at that point. After 3 seconds, however, something comes along an redraws the command line with /tmp appended again.

Expected behavior

After $ ls /tmp, I'd expect $ ls <escape> to leave me in vicmd mode with the command line being $ ls at that point and no remnant of the prior /tmp suggestion.

Desktop