Skip to content

Commit 4f2f17c

Browse files
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>
1 parent 0f80b8e commit 4f2f17c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ default values only after having loaded this script into your ZSH session.
141141
value, causes this script to perform a fuzzy search by words, matching in
142142
given order e.g. `ab c` will match `*ab*c*`
143143

144+
* `HISTORY_SUBSTRING_SEARCH_PREFIX` is a global variable that defines
145+
how the command history will be searched for your query. If set to a non-empty
146+
value, only history prefixed by your query will be matched. For example,
147+
if this variable is empty, `ls` will match `ls -l` and `echo ls`; if it is
148+
non-empty, `ls` will only match `ls -l`.
149+
144150
* `HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE` is a global variable that defines
145151
whether all search results returned are _unique_. If set to a non-empty
146152
value, then only unique search results are presented. This behaviour is off

zsh-history-substring-search.zsh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
4848
typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
4949
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
5050
typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=''
51+
typeset -g HISTORY_SUBSTRING_SEARCH_PREFIX=''
5152

5253
#-----------------------------------------------------------------------------
5354
# declare internal global variables
@@ -246,7 +247,8 @@ _history-substring-search-begin() {
246247
# Escape and join query parts with wildcard character '*' as seperator
247248
# `(j:CHAR:)` join array to string with CHAR as seperator
248249
#
249-
local search_pattern="*${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
250+
local search_pattern="${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
251+
test -z "$HISTORY_SUBSTRING_SEARCH_PREFIX" && search_pattern="*$search_pattern"
250252

251253
#
252254
# Find all occurrences of the search pattern in the history file.

0 commit comments

Comments
 (0)