From 7a0c87155b2c9f81db218f46eba36c9f60c71783 Mon Sep 17 00:00:00 2001 From: druckdev Date: Wed, 14 Jul 2021 23:57:23 +0200 Subject: [PATCH] Add `ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS` option With this the until now hard-coded value of 200 can be configured. --- README.md | 3 +++ src/config.zsh | 5 +++++ src/strategies/match_prev_cmd.zsh | 4 ++-- zsh-autosuggestions.zsh | 9 +++++++-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3cfd2e8..fb056b5 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ For more info, read the Character Highlighting section of the zsh manual: `man z For example, setting `ZSH_AUTOSUGGEST_STRATEGY=(history completion)` will first try to find a suggestion from your history, but, if it can't find a match, will find a suggestion from the completion engine. +#### When `ZSH_AUTOSUGGEST_STRATEGY` contains `match_prev_cmd`: + +- `ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS`: The previous command is only looked at of a number of the most recent commands that match the current prefix. This sets the maximum number of commands to consider. Set it to -1 to always use all matches. ### Widget Mapping diff --git a/src/config.zsh b/src/config.zsh index 5a0ebd8..aa78fe8 100644 --- a/src/config.zsh +++ b/src/config.zsh @@ -20,6 +20,11 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig- ZSH_AUTOSUGGEST_STRATEGY=(history) } +# Maximum number of commands to consider for match_prev_cmd strategy +# Set to -1 to always use all matches +(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) && +typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200 + # Widgets that clear the suggestion (( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && { typeset -ga ZSH_AUTOSUGGEST_CLEAR_WIDGETS diff --git a/src/strategies/match_prev_cmd.zsh b/src/strategies/match_prev_cmd.zsh index a2a852f..8c9a9fb 100644 --- a/src/strategies/match_prev_cmd.zsh +++ b/src/strategies/match_prev_cmd.zsh @@ -48,9 +48,9 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # Get the previously executed command local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" - # Iterate up to the first 200 history event numbers that match $prefix + # Iterate over the most recent history event numbers that match $prefix. local key - for key in "${(@)history_match_keys[1,200]}"; do + for key in "${(@)history_match_keys[1,$ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS]}"; do # Stop if we ran out of history [[ $key -gt 1 ]] || break diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 08a399f..0a2538b 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -46,6 +46,11 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig- ZSH_AUTOSUGGEST_STRATEGY=(history) } +# Maximum number of commands to consider for match_prev_cmd strategy +# Set to -1 to always use all matches +(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) && +typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200 + # Widgets that clear the suggestion (( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && { typeset -ga ZSH_AUTOSUGGEST_CLEAR_WIDGETS @@ -710,9 +715,9 @@ _zsh_autosuggest_strategy_match_prev_cmd() { # Get the previously executed command local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" - # Iterate up to the first 200 history event numbers that match $prefix + # Iterate over the most recent history event numbers that match $prefix. local key - for key in "${(@)history_match_keys[1,200]}"; do + for key in "${(@)history_match_keys[1,$ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS]}"; do # Stop if we ran out of history [[ $key -gt 1 ]] || break