1

Why will this

bindkey '^ ' end-of-line

works, and accept the suggestion, but

function accept {
  zle end-of-line
}
zle -N accept
bindkey '^ ' accept

this won't work.

I don't understand why. I also tried to replace end-of-line with autosuggest-accept in the function, but still nothing happens...


What I try to do is having two keybinding:

  • ctrl+space: accept the suggestion and execute the line easily done by bindkey '^ ' autosuggest-accept
  • ctrl+alt+space: accept the suggestion, add sudo at the beginning and execute. I tried this:
function insert-sudo-accept {
  zle end-of-line
  LBUFFER="sudo $LBUFFER"
  zle accept-line
}

But again as I said zle end-of-line does not work, please help.

0

1 Answer 1

1
  • When a keybinding’s widget is activated, the Zsh Line Editor (ZLE) sets the $WIDGET parameter to the name of the activated widget.
  • In turn, zsh-autosuggestions checks whether the value of $WIDGET is a member of $ZSH_AUTOSUGGEST_ACCEPT_WIDGETS. If so, it will accept the suggestion.
  • However, inside your widget function, when you call zle end-of-line, this will not change the value of $WIDGET.

There are three ways to solve this:

  • Bind zsh-autosuggestion’s own autosuggest-accept widget to CtrlSpace:
    bindkey '^@' autosuggest-accept
    
  • Call zle end-of-line -w. This will update $WIDGET to the name of the widget you’re calling.
  • Add your widget to $ZSH_AUTOSUGGEST_ACCEPT_WIDGETS:
    ZSH_AUTOSUGGEST_ACCEPT_WIDGETS+=(
        <insert widget name here>
    )
    
1
  • Thanks a lot, all that I needed was to register my widget name, ZSH_AUTOSUGGEST_ACCEPT_WIDGETS+=(insert-sudo-accept) Your answer helped me to find that out! Apr 16, 2023 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .