zsh-users / zsh-autosuggestions

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

zle widget based on `insert-last-word` no longer works as expected #748

Open Amar1729 opened 1 year ago

Amar1729 commented 1 year ago

Describe the bug

I have the following widget defined and bound in my rc (copied from this stackoverflow post):

insert-first-word () { zle insert-last-word -- -1 1 }
zle -N insert-first-word
bindkey '^[,' insert-first-word

This used to work as expected: when pressing alt + , (or escape ,), zsh would insert the first word of the previous command. However, it now inserts the last word of the previous command (commonly ^[., or insert-last-word (no argument)).

To Reproduce

Steps to reproduce the behavior:

% zsh -df
% source path/to/zsh-autosuggestions.zsh

# bind m+. (for comparison)
% bindkey '^[.' insert-last-word

# create widget and bind m+,
% insert-first-word () { zle insert-last-word -- -1 1 }
% zle -N insert-first-word
% bindkey '^[,' insert-first-word

# some command with arguments
% echo a b c

# at prompt, press m+. and m+,
# observe that both insert the word 'c', but the latter should insert 'echo'

If you do the above in the reverse order (bindkey commands first, then source), you'll notice that the keybinds work as expected before the source.

Expected behavior

As man zshzle indicates, this wrapper for insert-last-word -- -1 1 should insert the first word of the previous command.

Screenshots

Desktop

Tested with v0.2.0 as well, got:

_zsh_autosuggest_bound_insert-last-word:zle:2: unknown option: 1

Additional context

I'm quite confused how this used to work in my config and doesn't anymore. I haven't updated plugins recently, but apparently I must have changed something seemingly unrelated in my config. Will keep looking, but not sure I'll be able to track down what the issue is myself.

I also tried unset ZSH_AUTOSUGGEST_USE_ASYNC and export ZSH_AUTOSUGGEST_MANUAL_REBIND=1 (after sourcing the plugin), but neither of those seemed to make a difference.

Amar1729 commented 1 year ago

Slight addendum: as the SO post notes, the same behavior can be achieved built-in by alt+0 alt+. and indeed, bindkey -s '^[,' '\e0\e.' does properly insert the first word from the previous command while the plugin is sourced. So while this particular issue has a solution, I'm not sure if this indicates some deeper bug that could mess up something else.

huyz commented 6 months ago

Thanks, this saved me time to figure out what was wrong with my shell.

Disabling autosuggestions worked for me.

My best guess is that something changed with zsh 5.9. This issue seems to have started happening with zsh 5.9, which is the default version of the latest macOS 14.4.1 (Sonoma) and is the latest version installed by Homebrew. In contrast, the latest one for Ubuntu 22.04 is zsh 5.8.1, which may be why we haven't seen many people complain yet. And with zsh 5.8, there's no issue.