Codementor Events

CONFLICT BETWEEN ZSH-AUTOSUGGESTIONS PLUGIN AND COMMAND HISTORY NAVIGATION IN ZSH, SEEKING SOLUTION TO ALLOW CYCLING THROUGH HISTORY WHEN THERE IS NO MATCHING HISTORY FOR CURRENT COMMAND?

Published Mar 02, 2023
CONFLICT BETWEEN ZSH-AUTOSUGGESTIONS PLUGIN AND COMMAND HISTORY NAVIGATION IN ZSH, SEEKING SOLUTION TO ALLOW CYCLING THROUGH HISTORY WHEN THERE IS NO MATCHING HISTORY FOR CURRENT COMMAND?

Case #1:
using a zsh-autosuggestions plugin but there is a conflict occurring.

purpose of the plugin is to match what you have already typed and then search through the zsh_history file from that point for other commands that would match (starting from the last place your cursor was at),

conflict/issue: once you start hitting up/down arrows to cycle through commands your cursor gets moved to the end, then when you hit the left arrow key it moves the "match cursor" (not that actual name but thats what im calling it) to the end of the command as well

goal/what you want to happen instead: is if there is no more search history that matches what you have typed, then just cycle through the history like normal (as if nothing were typed at all) going from most recently run commands to least recently run.

Tech:

ohmyzsh

zsh-autosuggestions plugin

iterm2

Mac

you want to change the behavior of the zsh-autosuggestions plugin to allow for cycling through command history using the up and down arrow keys, even when there is no matching history for the current command.

To do this, you can modify the zsh-autosuggestions plugin configuration in your ~/.zshrc file. Here are the steps:

  1. Open your ~/.zshrc file in a text editor.

  2. Locate the line that loads the zsh-autosuggestions plugin. It should look something like this:

source /path/to/zsh-autosuggestions.plugin.zsh
  1. Add the following lines below the plugin loading line:
# disable the autosuggestion matching cursor when cycling through history
bindkey "^[[A" up-history
bindkey "^[[B" down-history
These lines remap the up and down arrow keys to the up-history and down-history Zsh functions, respectively, which allow you to cycle through command history without the autosuggestion matching cursor interfering.
  1. Save and close the ~/.zshrc file, then source it to apply the changes:
source ~/.zshrc

With these changes, you should be able to cycle through your command history using the up and down arrow keys, even when there is no matching history for the current command. Let me know if you have any further questions or issues.

Discover and read more posts from Anthony Elam
get started
post commentsBe the first to share your opinion
Show more replies