zsh-users / zsh-autosuggestions

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

Fix issue where partial accept duplicates last word #507

Open ericfreese opened 4 years ago

ericfreese commented 4 years ago

When z-sy-h is enabled after autosuggestion widgets have already been bound, partial accepting the last part of a suggestion will result in that string being duplicated.

I was able to reproduce using the following versions of the two plugins:

and running the following commands interactively one after another:

zsh -df
% source path/to/zsh-autosuggestions.zsh
% source path/to/zsh-syntax-highlighting.zsh
% bindkey -e
% bindkey <alt-f>         # shows 'bindkey -e-e'

The order of the source statements matters and the issue cannot be reproduced if the two source statements are executed together on one line.

The problem is very similar to this one: https://github.com/zsh-users/zsh-autosuggestions/issues/126#issuecomment-217121315

See GitHub issue #483

ericfreese commented 4 years ago

CI is being flaky since the async-by-default PR went in. It looks like with_history spec helper is not working right. Seems to be entering the history commands only partially.

tojin12341za commented 4 years ago

Duplicate of #

stevenxxiu commented 2 years ago

:+1: This fixes the issue for me, when it occurs when I use a custom widget:

$ zsh -df
$ source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
$ function key-cright() {
  zle vi-forward-word -- $@
}
zle -N key-cright
bindkey $'\e[1;5C' key-cright
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS+=(key-cright)

I'm not sure why unset POSTDISPLAY fixes the issue. Is it because POSTDISPLAY is used in _zsh_autosuggest_highlight_apply()?

arisu2718 commented 1 year ago

Thanks, your fix works for me. Didn't even think I could resolve this issue so fast :D

mojolo commented 1 year ago

Thumbs up to @ericfreese. In my case, this commit fixes the duplicating problem with a custom forward-word widget, even without any syntax-highlighting plugin loaded (see below).

The duplicating the last word upon partial acceptance of the last word issue has existed in various forms for several years now. It seems to happen to some people when using zsh-syntax-highlighting, fast-syntax-highlighting, vi-forward-word keybinds, or custom keybinds, etc.

This unset POSTDISPLAY commit seems to fix it in most/all cases. Is there a reason we can't get this commit pushed to the main branch?

  for-word-coarse() {
    local WORDCHARS='*?_-.[]~=/&:;!#$%^(){}<>'      # All chars are Wordchars (zsh default plus)
    zle forward-word
  }
  zle -N for-word-coarse
  bindkey '^[[1;5C' for-word-coarse                  # [Ctrl+Right] move forward to next non-blank character
  ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS+=(for-word-coarse)

EDIT: It turns out I didn't need to create the custom widget above and could just use the built-in vi-forward-blank-word widget to accomplish the same thing. Using the built-in widget, my duplicating the last word upon partial acceptance of the last word problem goes away. So, I'm a happy camper now. :) Nonetheless, it seems that custom widgets still cause issues with zsh-autosuggestions.

To sum up, the code above causes the duplicating the last word upon partial acceptance of the last word issue but the code below does not:

bindkey '^[[1;5C' vi-forward-blank-word             # [Ctrl+Right] move forward to next non-blank character