add HISTORY_SUBSTRING_SEARCH_PREFIX (#112,#115)

* `HISTORY_SUBSTRING_SEARCH_PREFIX` is a global variable that defines
  how the command history will be searched for your query. If set to a non-empty
  value, only history prefixed by your query will be matched. For example,
  if this variable is empty, `ls` will match `ls -l` and `echo ls`; if it is
  non-empty, `ls` will only match `ls -l`.

Co-authored-by: Xue Qianming <qianmingxue@microsoft.com>
This commit is contained in:
Xue Qianming 2021-08-03 01:20:14 +08:00 committed by GitHub
parent 0f80b8eb33
commit 4f2f17cc46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -141,6 +141,12 @@ default values only after having loaded this script into your ZSH session.
value, causes this script to perform a fuzzy search by words, matching in
given order e.g. `ab c` will match `*ab*c*`
* `HISTORY_SUBSTRING_SEARCH_PREFIX` is a global variable that defines
how the command history will be searched for your query. If set to a non-empty
value, only history prefixed by your query will be matched. For example,
if this variable is empty, `ls` will match `ls -l` and `echo ls`; if it is
non-empty, `ls` will only match `ls -l`.
* `HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE` is a global variable that defines
whether all search results returned are _unique_. If set to a non-empty
value, then only unique search results are presented. This behaviour is off

View file

@ -48,6 +48,7 @@ typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=''
typeset -g HISTORY_SUBSTRING_SEARCH_PREFIX=''
#-----------------------------------------------------------------------------
# declare internal global variables
@ -246,7 +247,8 @@ _history-substring-search-begin() {
# Escape and join query parts with wildcard character '*' as seperator
# `(j:CHAR:)` join array to string with CHAR as seperator
#
local search_pattern="*${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
local search_pattern="${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
test -z "$HISTORY_SUBSTRING_SEARCH_PREFIX" && search_pattern="*$search_pattern"
#
# Find all occurrences of the search pattern in the history file.