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:
Javi Merino
2022-05-12 07:42:17 +01:00
parent 76c6588204
commit 44b785e81b
2 changed files with 17 additions and 1 deletions

View File

@@ -44,6 +44,7 @@ plugins=(… zsh-fzf-history-search)
| `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | Put the cursor on at the end of the line after completion, `empty=false` (default: `''`) |
| `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | Include event numbers in search. Set to 0 to remove event numbers from the search. (default: `1`)|
| `ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH` | Include ISO8601 timestamps in search. Set to 0 to remove them from the search. (default: `1`) |
| `ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES`| Remove duplicate entries from search. Only makes sense with `EVENT_NUMBERS` and `DATE_INSEARCH` 0 (false). (default: ``) |
## TODO

View File

@@ -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]}}"