Skip to content

Commit

Permalink
add HISTORY_SUBSTRING_SEARCH_PREFIX (#112,#115)
Browse files Browse the repository at this point in the history
* `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 <[email protected]>
  • Loading branch information
SleepyBag and qianmingxue-msft authored Aug 2, 2021
1 parent 0f80b8e commit 4f2f17c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion zsh-history-substring-search.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

This comment has been minimized.

Copy link
@ericbn

ericbn Aug 2, 2021

Contributor

Shouldn't it be if [[ ... ]] instead of test here, since that is what's already being used throughout the existing code, plus Zsh has [[ exp ]] as one of its provided complex commands. This is not code to run in any shell, this is specifically for Zsh.

Also, the existing code seems to prefer

if condition; then
  list
fi

over if condition && list, which is not the same thing.

This comment has been minimized.

Copy link
@sunaku

sunaku Aug 3, 2021

Member

Good point. This has been addressed now in commit 4abed97. Cheers.

This comment has been minimized.

Copy link
@aabho

aabho Feb 7, 2022

Good


#
# Find all occurrences of the search pattern in the history file.
Expand Down

0 comments on commit 4f2f17c

Please sign in to comment.