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.
#### 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

View File

@ -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

View File

@ -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

View File

@ -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