mirror of
				https://github.com/zsh-users/zsh-autosuggestions.git
				synced 2025-10-30 15:26:29 +08:00 
			
		
		
		
	Add ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS option
				
					
				
			With this the number of preceding commands that have to match when using the `match_prev_cmd` strategy can be set (and can be multiple now).
This commit is contained in:
		
							parent
							
								
									7a0c87155b
								
							
						
					
					
						commit
						0d07421b42
					
				@ -60,7 +60,8 @@ For example, setting `ZSH_AUTOSUGGEST_STRATEGY=(history completion)` will first
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#### When `ZSH_AUTOSUGGEST_STRATEGY` contains `match_prev_cmd`:
 | 
					#### 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.
 | 
					- `ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS`: The previous commands are 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.
 | 
				
			||||||
 | 
					- `ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS`: Number of previous commands that should match. Setting this to a value below 1 results in the strategy `history` to be simulated (with extra steps).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Widget Mapping
 | 
					### Widget Mapping
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -25,6 +25,10 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
 | 
				
			|||||||
(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) &&
 | 
					(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) &&
 | 
				
			||||||
typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200
 | 
					typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Number of previous commands that should match.
 | 
				
			||||||
 | 
					(( ! ${+ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS} )) &&
 | 
				
			||||||
 | 
					typeset -g ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS=1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 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
 | 
				
			||||||
 | 
				
			|||||||
@ -2,9 +2,9 @@
 | 
				
			|||||||
#--------------------------------------------------------------------#
 | 
					#--------------------------------------------------------------------#
 | 
				
			||||||
# Match Previous Command Suggestion Strategy                         #
 | 
					# Match Previous Command Suggestion Strategy                         #
 | 
				
			||||||
#--------------------------------------------------------------------#
 | 
					#--------------------------------------------------------------------#
 | 
				
			||||||
# Suggests the most recent history item that matches the given
 | 
					# Suggests the most recent history item that all_match the given
 | 
				
			||||||
# prefix and whose preceding history item also matches the most
 | 
					# prefix and whose preceding history items also all_match the most
 | 
				
			||||||
# recently executed command.
 | 
					# recently executed commands.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# For example, suppose your history has the following entries:
 | 
					# For example, suppose your history has the following entries:
 | 
				
			||||||
#   - pwd
 | 
					#   - pwd
 | 
				
			||||||
@ -16,6 +16,9 @@
 | 
				
			|||||||
# will be 'ls foo' rather than 'ls bar' because your most recently
 | 
					# will be 'ls foo' rather than 'ls bar' because your most recently
 | 
				
			||||||
# executed command (pwd) was previously followed by 'ls foo'.
 | 
					# executed command (pwd) was previously followed by 'ls foo'.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					# You can customize how many commands have to match by setting
 | 
				
			||||||
 | 
					# `ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS`.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
# Note that this strategy won't work as expected with ZSH options that don't
 | 
					# Note that this strategy won't work as expected with ZSH options that don't
 | 
				
			||||||
# preserve the history order such as `HIST_IGNORE_ALL_DUPS` or
 | 
					# preserve the history order such as `HIST_IGNORE_ALL_DUPS` or
 | 
				
			||||||
# `HIST_EXPIRE_DUPS_FIRST`.
 | 
					# `HIST_EXPIRE_DUPS_FIRST`.
 | 
				
			||||||
@ -45,18 +48,30 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
 | 
				
			|||||||
	# By default we use the first history number (most recent history entry)
 | 
						# By default we use the first history number (most recent history entry)
 | 
				
			||||||
	local histkey="${history_match_keys[1]}"
 | 
						local histkey="${history_match_keys[1]}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# Get the previously executed command
 | 
						# Get the previously executed commands
 | 
				
			||||||
	local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
 | 
						local -a prev_cmds
 | 
				
			||||||
 | 
						local i
 | 
				
			||||||
 | 
						for ((i = 1; i <= $ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS; i++)); do
 | 
				
			||||||
 | 
							prev_cmds+="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-i))]}")"
 | 
				
			||||||
 | 
						done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# Iterate over the most recent history event numbers that match $prefix.
 | 
						# Iterate over the most recent history event numbers that match $prefix.
 | 
				
			||||||
	local key
 | 
						local key all_match
 | 
				
			||||||
	for key in "${(@)history_match_keys[1,$ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS]}"; 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		# See if the history entry preceding the suggestion matches the
 | 
							# See if the history entries preceding the suggestion match the previous
 | 
				
			||||||
		# previous command, and use it if it does
 | 
							# commands, and use it if they do
 | 
				
			||||||
		if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
 | 
							all_match=1
 | 
				
			||||||
 | 
							for ((i = 1; i <= $ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS; i++)); do
 | 
				
			||||||
 | 
								if [[ "${history[$((key - i))]}" != "$prev_cmds[i]" ]]; then
 | 
				
			||||||
 | 
									all_match=0
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								fi
 | 
				
			||||||
 | 
							done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (( all_match )); then
 | 
				
			||||||
			histkey="$key"
 | 
								histkey="$key"
 | 
				
			||||||
			break
 | 
								break
 | 
				
			||||||
		fi
 | 
							fi
 | 
				
			||||||
 | 
				
			|||||||
@ -51,6 +51,10 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
 | 
				
			|||||||
(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) &&
 | 
					(( ! ${+ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS} )) &&
 | 
				
			||||||
typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200
 | 
					typeset -g ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS=200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Number of previous commands that should match.
 | 
				
			||||||
 | 
					(( ! ${+ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS} )) &&
 | 
				
			||||||
 | 
					typeset -g ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS=1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 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
 | 
				
			||||||
@ -669,9 +673,9 @@ _zsh_autosuggest_strategy_history() {
 | 
				
			|||||||
#--------------------------------------------------------------------#
 | 
					#--------------------------------------------------------------------#
 | 
				
			||||||
# Match Previous Command Suggestion Strategy                         #
 | 
					# Match Previous Command Suggestion Strategy                         #
 | 
				
			||||||
#--------------------------------------------------------------------#
 | 
					#--------------------------------------------------------------------#
 | 
				
			||||||
# Suggests the most recent history item that matches the given
 | 
					# Suggests the most recent history item that all_match the given
 | 
				
			||||||
# prefix and whose preceding history item also matches the most
 | 
					# prefix and whose preceding history items also all_match the most
 | 
				
			||||||
# recently executed command.
 | 
					# recently executed commands.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# For example, suppose your history has the following entries:
 | 
					# For example, suppose your history has the following entries:
 | 
				
			||||||
#   - pwd
 | 
					#   - pwd
 | 
				
			||||||
@ -683,6 +687,9 @@ _zsh_autosuggest_strategy_history() {
 | 
				
			|||||||
# will be 'ls foo' rather than 'ls bar' because your most recently
 | 
					# will be 'ls foo' rather than 'ls bar' because your most recently
 | 
				
			||||||
# executed command (pwd) was previously followed by 'ls foo'.
 | 
					# executed command (pwd) was previously followed by 'ls foo'.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					# You can customize how many commands have to match by setting
 | 
				
			||||||
 | 
					# `ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS`.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
# Note that this strategy won't work as expected with ZSH options that don't
 | 
					# Note that this strategy won't work as expected with ZSH options that don't
 | 
				
			||||||
# preserve the history order such as `HIST_IGNORE_ALL_DUPS` or
 | 
					# preserve the history order such as `HIST_IGNORE_ALL_DUPS` or
 | 
				
			||||||
# `HIST_EXPIRE_DUPS_FIRST`.
 | 
					# `HIST_EXPIRE_DUPS_FIRST`.
 | 
				
			||||||
@ -712,18 +719,30 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
 | 
				
			|||||||
	# By default we use the first history number (most recent history entry)
 | 
						# By default we use the first history number (most recent history entry)
 | 
				
			||||||
	local histkey="${history_match_keys[1]}"
 | 
						local histkey="${history_match_keys[1]}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# Get the previously executed command
 | 
						# Get the previously executed commands
 | 
				
			||||||
	local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")"
 | 
						local -a prev_cmds
 | 
				
			||||||
 | 
						local i
 | 
				
			||||||
 | 
						for ((i = 1; i <= $ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS; i++)); do
 | 
				
			||||||
 | 
							prev_cmds+="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-i))]}")"
 | 
				
			||||||
 | 
						done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# Iterate over the most recent history event numbers that match $prefix.
 | 
						# Iterate over the most recent history event numbers that match $prefix.
 | 
				
			||||||
	local key
 | 
						local key all_match
 | 
				
			||||||
	for key in "${(@)history_match_keys[1,$ZSH_AUTOSUGGEST_MATCH_PREV_MAX_CMDS]}"; 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		# See if the history entry preceding the suggestion matches the
 | 
							# See if the history entries preceding the suggestion match the previous
 | 
				
			||||||
		# previous command, and use it if it does
 | 
							# commands, and use it if they do
 | 
				
			||||||
		if [[ "${history[$((key - 1))]}" == "$prev_cmd" ]]; then
 | 
							all_match=1
 | 
				
			||||||
 | 
							for ((i = 1; i <= $ZSH_AUTOSUGGEST_MATCH_NUM_PREV_CMDS; i++)); do
 | 
				
			||||||
 | 
								if [[ "${history[$((key - i))]}" != "$prev_cmds[i]" ]]; then
 | 
				
			||||||
 | 
									all_match=0
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								fi
 | 
				
			||||||
 | 
							done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (( all_match )); then
 | 
				
			||||||
			histkey="$key"
 | 
								histkey="$key"
 | 
				
			||||||
			break
 | 
								break
 | 
				
			||||||
		fi
 | 
							fi
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user