zsh-users / zsh-autosuggestions

Fish-like autosuggestions for zsh
MIT License
30.43k stars 1.85k forks source link

Navigation through autosuggestions not working #594

Open ivallesp opened 3 years ago

ivallesp commented 3 years ago

I have installed zsh-autosuggestions using zinit on my zsh in Ubuntu 20.04 and everything is working but I cannot navigate through the autosuggestions. When I type git clone it suggests me the last history entry where I used that command (as expected), then I hit the up arrow key, and it does not jump to the next autosuggestion, but to the previous command. This functionality was working in my mac but I cannot make it work in Ubuntu, Any clues?

I install the plugin by appending the following command to my .zshrc file.

zinit load zsh-users/zsh-autosuggestions
ivallesp commented 3 years ago

I've found a workaround

Add this to your .zshrc

# Fix zsh autosuggestions keybind for arrow keys
zle-line-init() {}
bindkey '\e[A' history-beginning-search-backward
bindkey '\e[B' history-beginning-search-forward
iambeingtracked commented 2 years ago

The solutions you've written above works. But the cursor stays where you typed the last letter before pressing the up arrow. Is there a way to fix it?

0x303 commented 1 year ago

I am having the same problem with the cursor not going to the end of the line with RIGHT arrow key press (or automatically when scrolling) but this solution was working for me oterwise.

faloi commented 1 year ago

Did you find any way to improve this?

iambeingtracked commented 1 year ago

I didn't maybe @0x303 did, but it worked properly on oh-my-zsh for some reason. And with a manual install it doesn't, honestly weird

speedytwenty commented 5 months ago

I'd been searching for the issue here attempting to ditch oh-my-zsh—without ditching my autosuggest keybindings.

Noticing the same behavior as @iambeingtracked (#metoo) where it worked as expected with oh-my-zsh but not with a barebones install, I was about to give up.

The keywords @ivallesp provided allowed me to track down what and where this is occuring in oh-my-zsh

For my purposes, I was able to widdle this down to:

# Make sure that the terminal is in application mode when zle is active, since
# only then values from $terminfo are valid

if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  function zle-line-init() {
    echoti smkx
  }
  function zle-line-finish() {
    echoti rmkx
  }
  zle -N zle-line-init
  zle -N zle-line-finish
fi

# [PageUp] - Up a line of history
if [[ -n "${terminfo[kpp]}" ]]; then
  bindkey -M viins "${terminfo[kpp]}" up-line-or-history
  bindkey -M vicmd "${terminfo[kpp]}" up-line-or-history
fi
# [PageDown] - Down a line of history
if [[ -n "${terminfo[knp]}" ]]; then
  bindkey -M viins "${terminfo[knp]}" down-line-or-history
  bindkey -M vicmd "${terminfo[knp]}" down-line-or-history
fi
# Start typing + [Up-Arrow] - fuzzy find history forward
if [[ -n "${terminfo[kcuu1]}" ]]; then
  autoload -U up-line-or-beginning-search
  zle -N up-line-or-beginning-search

  bindkey -M viins "${terminfo[kcuu1]}" up-line-or-beginning-search
  bindkey -M vicmd "${terminfo[kcuu1]}" up-line-or-beginning-search
fi
# Start typing + [Down-Arrow] - fuzzy find history backward
if [[ -n "${terminfo[kcud1]}" ]]; then
  autoload -U down-line-or-beginning-search
  zle -N down-line-or-beginning-search

  bindkey -M viins "${terminfo[kcud1]}" down-line-or-beginning-search
  bindkey -M vicmd "${terminfo[kcud1]}" down-line-or-beginning-search
fi
iambeingtracked commented 5 months ago

The above works, but it doesn't if bindkey -e is set

speedytwenty commented 5 months ago

The above works, but it doesn't if bindkey -e is set

Whether I include or exclude this and the emacs bindings has no effect for me on MacOS. The oh-my-zsh code that I linked to includes bindkey -e around line ~19. Your reports (that omz works but bindkey -e does not) seem to conflict or indicate variations that I'm not sure how to isolate.

If omz still works for you, you might try the entire [key-bindings.vim] (https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/key-bindings.zsh) file from omz.

speedytwenty commented 5 months ago

This is the quickest way to try this if you've got oh-my-zsh installed.

~/.zshrc

source ~/.oh-my-zsh/zsh-autosuggestions/zsh-autosuggestions.plugin.zsh
source ~/.oh-my-zsh/lib/key-bindings.zsh

For what it's worth: I find that I'm never quite sure I've actually tested hacks to the shell unless I fully restart the terminal (or observe an affect). I strive to test with an alternate terminal application when making changes to my shell.

iambeingtracked commented 5 months ago

The above works, but it doesn't if bindkey -e is set

Whether I include or exclude this and the emacs bindings has no effect for me on MacOS. The oh-my-zsh code that I linked to includes bindkey -e around line ~19. Your reports (that omz works but bindkey -e does not) seem to conflict or indicate variations that I'm not sure how to isolate.

If omz still works for you, you might try the entire [key-bindings.vim] (https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/key-bindings.zsh) file from omz.

I haven't used omz in a very long time, I don't even know how to use it. bindkey -e does change things, try ctrl-a and ctrl-e with and without it. Also, I tried pasting the entirety of key-bindings.zsh but then zsh-autosuggestions works in an even weirder way, it doesn't enter the command that shows up, but rather what it finds when it searches for all commands that start with what is entered in the command line. And it will keep searching, even if you start entering the same after erasing what you wrote it will keep searching from the place it stopped