Add an option to remove duplicates from search
Some users may prefer to keep duplicates in their zsh history, but drop them from search. The awk manual[0] recommends this. [0] https://www.gnu.org/software/gawk/manual/html_node/History-Sorting.html
This commit is contained in:
@@ -25,6 +25,10 @@ typeset -g ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS=1
|
||||
(( ! ${+ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH} )) &&
|
||||
typeset -g ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH=1
|
||||
|
||||
# Remove duplicate entries in history
|
||||
(( ! ${+ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES} )) &&
|
||||
typeset -g ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES=''
|
||||
|
||||
fzf_history_search() {
|
||||
setopt extendedglob
|
||||
|
||||
@@ -41,7 +45,18 @@ fzf_history_search() {
|
||||
((CANDIDATE_LEADING_FIELDS+=2))
|
||||
fi
|
||||
|
||||
candidates=(${(f)"$(fc ${=FC_ARGS} -1 0 | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"})
|
||||
history_cmd="fc ${=FC_ARGS} -1 0"
|
||||
|
||||
if [ -n "${ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES}" ];then
|
||||
if (( $+commands[awk] )); then
|
||||
history_cmd="$history_cmd | awk '!seen[\$0]++'"
|
||||
else
|
||||
# In case awk is not installed fallback to uniq. It will only remove commands that are repeated consecutively.
|
||||
history_cmd="$history_cmd | uniq"
|
||||
fi
|
||||
fi
|
||||
|
||||
candidates=(${(f)"$(eval $history_cmd | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"})
|
||||
local ret=$?
|
||||
if [ -n "$candidates" ]; then
|
||||
BUFFER="${candidates[@]/(#m)*/${${(As: :)MATCH}[${CANDIDATE_LEADING_FIELDS},-1]}}"
|
||||
|
||||
Reference in New Issue
Block a user