Add ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS option

With this the until now hard-coded value of 200 can be configured.
This commit is contained in:
druckdev 2021-07-14 23:57:23 +02:00
parent afee79ef5b
commit 7a0c87155b
No known key found for this signature in database
GPG key ID: CA6B3A516FAC2555
4 changed files with 17 additions and 4 deletions

View file

@ -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. 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 ### Widget Mapping

View file

@ -20,6 +20,11 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
ZSH_AUTOSUGGEST_STRATEGY=(history) 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 # Widgets that clear the suggestion
(( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && { (( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && {
typeset -ga ZSH_AUTOSUGGEST_CLEAR_WIDGETS typeset -ga ZSH_AUTOSUGGEST_CLEAR_WIDGETS

View file

@ -48,9 +48,9 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
# Get the previously executed command # Get the previously executed command
local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" 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 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 # Stop if we ran out of history
[[ $key -gt 1 ]] || break [[ $key -gt 1 ]] || break

View file

@ -46,6 +46,11 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
ZSH_AUTOSUGGEST_STRATEGY=(history) 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 # Widgets that clear the suggestion
(( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && { (( ! ${+ZSH_AUTOSUGGEST_CLEAR_WIDGETS} )) && {
typeset -ga 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 # Get the previously executed command
local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" 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 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 # Stop if we ran out of history
[[ $key -gt 1 ]] || break [[ $key -gt 1 ]] || break